Retrofit2 使用FastJson作为Converter

首先创建一个FastJsonRequestBodyConverter类

package com.rrc.core.net.converter;

import com.alibaba.fastjson.JSON;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Converter;

/**
 * ================================================
 *
 * @Description: 描述
 * @Author: wxianing
 * @Date: 2024/7/16 14:35
 * ================================================
 */
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类

package com.rrc.core.net.converter;

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;

/**
 * ================================================
 *
 * @Description: 描述
 * @Author: wxianing
 * @Date: 2024/7/16 14:34
 * ================================================
 */
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);

    }
}

接着创建FastJsonConverterFactory类

package com.rrc.core.net.converter;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * ================================================
 *
 * @Description: 描述
 * @Author: wxianing
 * @Date: 2024/7/16 14:33
 * ================================================
 */
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<>();
    }
}

然后在获取Retrofit实例时添加

 mRetrofit = Retrofit.Builder()
                    .baseUrl(Constants.CONSTANTS_APP_DOMAIN)
                    .client(getOkHttpClient())
//                    .addConverterFactory(BaseConverterFactory.create())
                    .addConverterFactory(FastJsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build()

这样Retrofit就可以使用Fastjson解析数据了

posted @ 2024-07-16 15:11  い果粒橙ぺ  阅读(9)  评论(0编辑  收藏  举报