zoukankan      html  css  js  c++  java
  • Retrofit动态设置支持JSON和XML格式转换工厂

    @

    日常开发中,网络请求一般数据传输协议格式一般都是固定的,JSON或XML等。但总有一些例外,一个项目中有多种格式,也算是Android开发人员比较头疼的了。

    Retrofit-Converter.Factory转换工厂

    Retrofit是常用且功能强大的网络请求框架,通过Converter.Factory可以将Bean转为RequestBody,ResponseBody转为Bean。
    官方也提供了一些转换工厂,供我们快速开发:retrofit-converters

    导入相关依赖

    implementation 'com.squareup.retrofit2:retrofit:2.6.2'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
    implementation 'com.squareup.retrofit2:converter-simplexml:2.6.2'
    

    版本号可替换成最新版本。
    提示:SimpleXml已经被官方弃用,官方推荐使用JAXB,当时测试JAXB使用时报错。converter-jaxb

    创建ConverterFormat枚举类

    /**
     * 数据解析的方式
     * json或者xml
     */
    enum class ConverterFormat {
        JSON,
        XML
    }
    

    声明RequestConverter注解

    @Target(
        AnnotationTarget.FUNCTION
    )
    @Retention(AnnotationRetention.RUNTIME)
    @MustBeDocumented
    annotation class RequestConverter(val format: ConverterFormat = ConverterFormat.JSON)
    

    默认JSON格式。

    声明ResponseConverter注解

    @Target(
        AnnotationTarget.FUNCTION
    )
    @Retention(AnnotationRetention.RUNTIME)
    @MustBeDocumented
    annotation class ResponseConverter(val format: ConverterFormat = ConverterFormat.JSON)
    

    默认JSON格式。

    自定义JsonOrXmlConverterFactory

    class JsonOrXmlConverterFactory private constructor() : Converter.Factory() {
        private var jsonFactory: Converter.Factory
        private var xmlFactory: Converter.Factory
    
        init {
            val gson = GsonBuilder()
                .serializeNulls()
                .create()
            jsonFactory = GsonConverterFactory.create(gson)
    
            xmlFactory = SimpleXmlConverterFactory.createNonStrict()
        }
    
        companion object {
            fun create() = JsonOrXmlConverterFactory()
        }
    
        override fun requestBodyConverter(
            type: Type,
            parameterAnnotations: Array<Annotation>,
            methodAnnotations: Array<Annotation>,
            retrofit: Retrofit
        ): Converter<*, RequestBody>? {
            for (annotation in methodAnnotations) {
                if (annotation is RequestConverter) {
                    if (annotation.format == ConverterFormat.JSON) {
                        return jsonFactory.requestBodyConverter(
                            type,
                            parameterAnnotations,
                            methodAnnotations,
                            retrofit
                        )
                    } else if (annotation.format == ConverterFormat.XML) {
                        return xmlFactory.requestBodyConverter(
                            type,
                            parameterAnnotations,
                            methodAnnotations,
                            retrofit
                        )
                    }
                }
            }
            return jsonFactory.requestBodyConverter(
                type,
                parameterAnnotations,
                methodAnnotations,
                retrofit
            )
        }
    
        override fun responseBodyConverter(
            type: Type,
            annotations: Array<Annotation>,
            retrofit: Retrofit
        ): Converter<ResponseBody, *>? {
            for (annotation in annotations) {
                if (annotation is ResponseConverter) {
                    if (annotation.format == ConverterFormat.JSON) {
                        return jsonFactory.responseBodyConverter(type, annotations, retrofit)
                    } else if (annotation.format == ConverterFormat.XML) {
                        return xmlFactory.responseBodyConverter(type, annotations, retrofit)
                    }
                }
            }
            return jsonFactory.responseBodyConverter(type, annotations, retrofit)
        }
    }
    

    如果没找到相关注解,则使用JSON格式。

    使用方法

    1. 创建Retrofit实例时通过addConverterFactory添加JsonOrXmlConverterFactory
    fun init() {
        val retrofitBuilder = Retrofit.Builder()
        	.addConverterFactory(JsonOrXmlConverterFactory.create())
        val retrofit = retrofitBuilder.build()
        val apiService = retrofit.create(ApiService::class.java)
    }
    
    1. 在接口上添加注解
    @POST
    @RequestConverter(ConverterFormat.XML)
    @ResponseConverter(ConverterFormat.JSON)
    @Headers("Connection: Close")
    fun netRelayCtrl(@Url url: String, @Body bean: NetRelayCtrlBean): Observable<NetRelayCtrlResultBean>
    

    不添加RequestConverter、ResponseConverter注解,则默认使用JSON解析。如有错误,还望指正。

  • 相关阅读:
    phpstorm 中文版 支持BUG调试 IDE
    WINDOWS中设置计划任务执行PHP文件
    数据库 Navicat_Premium_11.0.10 破解版下载安装
    zend Studio10.6.2破解注册码
    zend Studio10.6.2 下载
    【jdbcTemplate】使用jdbcTemplate查询的三种回调
    【JDBC】向数据表插入数据时,自动获取生成的主键
    【转】JDBC为什么要使用PreparedStatement而不是Statement
    【Spring学习笔记-6】关于@Autowired与@Scope(BeanDefination.SCOPE_PROTOTYPE)
    【java基础学习-2--】关于Hashcode()的使用
  • 原文地址:https://www.cnblogs.com/qq714081644/p/12732779.html
Copyright © 2011-2022 走看看