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);
0 0