Retrofit自定义CONVERTERS(fastjson)

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值