retrofit动态设置json或xml或其他转换工厂

    xiaoxiao2023-11-08  139

    文章目录

    retrofit转换工厂介绍可以混用的转换工厂CompositeConverterFactory声明工厂CompositeConverterFactory声明注解@RequestConverter声明注解@ResponseConverterCompositeConverterFactory的使用@RequestConverter @ResponseConverter 的使用:case1 接口返回的json数据转为对象case2 获取接口返回的原始数据(字符串) json 或 xml 或 其他case3 把javabean转为json字符串以post方式上传到服务器端 在gradle中添加依赖即可使用

    retrofit转换工厂介绍

    retrofit是个强大的工具,而retrofit converter可以把请求的结果转为可供我们直接使用的java bean对象。如果不使用转换工厂retrofit则请求方法回只能为ResponseBody对象。 官方为我们提供了一些默认的转换工厂:https://github.com/square/retrofit/tree/master/retrofit-converters

    public interface GitHubService { @GET("users/{user}") Call<User> getUserInfo(@Path("user") String user); }

    要使用工厂就要在retrofit实例设置转换工厂:

    Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .build();

    这里设置了一个GsonConverterFactory,它可以把http请求响应json数据转为java bean;这里json数据被反序列化为User对象了;但是这个retrofit对象就只能处理gson数据了;

    一般服务器接口所返回的数据结构都是统一规范的,比如返回数据结构为JSON;那如果有特殊情况怎么办?比如服务中有个接口返回xml数据,上面的retrofit实例就不能用了,只能从新创建了;

    可以混用的转换工厂CompositeConverterFactory

    声明工厂CompositeConverterFactory

    自定义一个可以混用的转换工厂,它自己并不处理数据,给它设置默认工厂来处理数据,比如GsonConverterFactory就处理json数据,设置SimpleXmlConverterFactory就处理xml数据;并且还可以在请求方法上设置注解来动态设置转换工厂;

    CompositeConverterFactory.java

    public class CompositeConverterFactory extends Converter.Factory { private Converter.Factory mFactory; public static CompositeConverterFactory create(Converter.Factory factory) { if (factory == null) { throw new NullPointerException("parameter is null"); } else { return new CompositeConverterFactory(factory); } } private CompositeConverterFactory(Converter.Factory factory) { this.mFactory = factory; } @Override public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { Class<?> factoryClass = null; for (Annotation annotation : annotations) { if (annotation instanceof ResponseConverter) { factoryClass = ((ResponseConverter) annotation).value(); break; } } Converter.Factory factory = null; if (factoryClass != null) { try { Method createMethod = factoryClass.getMethod("create"); factory = (Converter.Factory) createMethod.invoke(null); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } if (factory == null && mFactory != null) { factory = mFactory; } if (factory != null) return factory.responseBodyConverter(type, annotations, retrofit); return super.responseBodyConverter(type, annotations, retrofit); } @Override public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { Class<?> factoryClass = null; for (Annotation paramAnno : methodAnnotations) { if (paramAnno instanceof RequestConverter) { factoryClass = ((RequestConverter) paramAnno).value(); break; } } Converter.Factory factory = null; if (factoryClass != null) { try { Method createMethod = factoryClass.getMethod("create"); factory = (Converter.Factory) createMethod.invoke(null); return factory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } if (factory == null && mFactory != null) { factory = mFactory; } if (factory != null) return factory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit); return super.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit); } }

    声明注解@RequestConverter

    @RequestConverter

    @Documented @Target({METHOD}) @Retention(RUNTIME) public @interface RequestConverter { Class<? extends Converter.Factory> value(); }

    声明注解@ResponseConverter

    @ResponseConverter

    @Documented @Target({METHOD}) @Retention(RUNTIME) public @interface ResponseConverter { Class<? extends Converter.Factory> value(); }

    CompositeConverterFactory的使用

    使用自定义转换工厂:

    Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(CompositeConverterFactory.create(GsonConverterFactory.create())) .build();

    这里默认设置GsonConverterFactory,默认处理json数据结构的数据。CompositeConverterFactory转换什么数据由传入的默认工厂决定。

    @RequestConverter @ResponseConverter 的使用:

    CompositeConverterFactory.create(factory)传入参数为默认工厂;

    case1 接口返回的json数据转为对象

    public interface GitHubService { @ResponseConverter(GsonConverterFactory.class) @GET("users/{user}") Call<User> getUserInfo(@Path("user") String user); }

    case2 获取接口返回的原始数据(字符串) json 或 xml 或 其他

    @ResponseConverter(ScalarsConverterFactory.class) @GET("users/{user}") Call<String> getUserInfo2(@Path("user") String user);

    case3 把javabean转为json字符串以post方式上传到服务器端

    @RequestConverter(GsonConverterFactory.class) @POST("/uploaddata") Call<String> upload(@Body User user);

    是不是很简单?It is easy!!!

    在gradle中添加依赖即可使用

    repositories { jcenter() } dependencies { implementation 'com.github.woodyhi.retrofit:composite-converter:0.1.3' }

    PS:源码 https://github.com/woodyhi/retrofit-converter 其中有test代码来演示。

    最新回复(0)