Retrofit自定义CONVERTERS(fastjson)
来源:互联网 发布:阿里云怎么选择刷机包 编辑:程序博客网 时间:2024/05/16 15:12
Retortfit
A type-safe HTTP client for Android and Java
CONVERTERS
Converters can be added to support other types. Six sibling modules adapt popular serialization libraries for your convenience.
- Gson: com.squareup.retrofit2:converter-gson Jackson:
- com.squareup.retrofit2:converter-jackson Moshi:
- com.squareup.retrofit2:converter-moshi Protobuf:
- com.squareup.retrofit2:converter-protobuf Wire:
- com.squareup.retrofit2:converter-wire Simple XML:
- com.squareup.retrofit2:converter-simplexml Scalars (primitives,
- boxed, and String): com.squareup.retrofit2:converter-scalars
CUSTOM CONVERTERS
If you need to communicate with an API that uses a content-format that
Retrofit does not support out of the box (e.g. YAML, txt, custom
format) or you wish to use a different library to implement an
existing format, you can easily create your own converter. Create a
class that extends the Converter.Factory class and pass in an instance
when building your adapter.
仔细一看了下,并没有我们熟知的fastjson。但是文档中告诉了我们该如何去自定一个converter,下面一起来看下怎么扩展。
首先在module的build.gradle中添加fastjson
compile ‘com.alibaba:fastjson:1.2.31’
然后创建FastJsonConverterFactory 类,并继承Converter.Factory,重写其中的responseBodyConverter方法与requestBodyConverter方法。
import java.lang.annotation.Annotation;import java.lang.reflect.Type;import okhttp3.RequestBody;import okhttp3.ResponseBody;import retrofit2.Converter;import retrofit2.Retrofit;/** * Created by dawN4get on 2017/5/14. */public class FastJsonConverterFactory extends Converter.Factory{ public static FastJsonConverterFactory create() { return new FastJsonConverterFactory(); } /** * 需要重写父类中responseBodyConverter,该方法用来转换服务器返回数据 */ @Override public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { return new FastJsonResponseBodyConverter<>(type); } /** * 需要重写父类中responseBodyConverter,该方法用来转换发送给服务器的数据 */ @Override public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { return new FastJsonRequestBodyConverter<>(); }}
创建FastJsonResquestBodyConverter 类:
import com.alibaba.fastjson.JSON;import java.io.IOException;import okhttp3.MediaType;import okhttp3.RequestBody;import retrofit2.Converter;/** * Created by dawN4get on 2017/5/14. */public class FastJsonRequestBodyConverter<T> implements Converter<T, RequestBody> { private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8"); @Override public RequestBody convert(T value) throws IOException { return RequestBody.create(MEDIA_TYPE, JSON.toJSONBytes(value)); }}
创建FastJsonResponseBodyConverter 类:
import com.alibaba.fastjson.JSON;import java.io.IOException;import java.lang.reflect.Type;import okhttp3.ResponseBody;import okio.BufferedSource;import okio.Okio;import retrofit2.Converter;/** * Created by dawN4get on 2017/5/14. */public class FastJsonResponseBodyConverter<T> implements Converter<ResponseBody, T> { private final Type type; public FastJsonResponseBodyConverter(Type type) { this.type = type; } /* * 转换方法 */ @Override public T convert(ResponseBody value) throws IOException { BufferedSource bufferedSource = Okio.buffer(value.source()); String tempStr = bufferedSource.readUtf8(); bufferedSource.close(); return JSON.parseObject(tempStr, type); }}
Use FastJsonConverterFactory
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com") .addConverterFactory(FastJsonConverterFactory .create()) .build();GitHubService service = retrofit.create(GitHubService.class);
- Retrofit自定义CONVERTERS(fastjson)
- Retrofit+fastjson
- Retrofit源码学习笔记(3)-Converters解析
- FastJson 自定义Serialize、Parser
- Fastjson 自定义输出
- FastJson Date类型自定义
- fastjson自定义序列化
- Retrofit 自定义Cookies
- Retrofit 2.0 自定义Converter
- Retrofit自定义Converter
- Retrofit自定义Converter步骤
- DWR Converters
- dwr-converters
- 自定义classloader装载fastjson包
- SpringMVC+FastJson 自定义日期转换器
- fastjson @JSONField自定义输出字段
- fastjson @JSONField自定义输出字段
- SpringMVC+FastJson 自定义日期转换器
- 我的开发成果截图
- [PAT-乙级]1018.锤子剪刀布
- 使用Java实现简易RPC框架
- 这不是装x扯犊子吗
- 使用spring的@ResponseBody注解报Not Acceptable错误
- Retrofit自定义CONVERTERS(fastjson)
- Zookeeper系列(二十八)Zookeeper之通讯模型
- react block
- PAT天梯赛 5-19 阅览室
- JAVA获取当前系统时间System.currentTimeMillis()
- 根据日期判断天数(难度:半颗星)
- Java实现贪吃蛇(一)
- mySql字段设置varchar的时间比较
- Redis源码剖析和注释(二十)--- 网络连接库剖析(client的创建/释放、命令接收/回复、Redis通信协议分析等)