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实例就不能用了,只能从新创建了;
自定义一个可以混用的转换工厂,它自己并不处理数据,给它设置默认工厂来处理数据,比如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
@Documented @Target({METHOD}) @Retention(RUNTIME) public @interface RequestConverter { Class<? extends Converter.Factory> value(); }@ResponseConverter
@Documented @Target({METHOD}) @Retention(RUNTIME) public @interface ResponseConverter { Class<? extends Converter.Factory> value(); }使用自定义转换工厂:
Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(CompositeConverterFactory.create(GsonConverterFactory.create())) .build();这里默认设置GsonConverterFactory,默认处理json数据结构的数据。CompositeConverterFactory转换什么数据由传入的默认工厂决定。
CompositeConverterFactory.create(factory)传入参数为默认工厂;
是不是很简单?It is easy!!!
PS:源码 https://github.com/woodyhi/retrofit-converter 其中有test代码来演示。