Retrofit自定義請(qǐng)求轉(zhuǎn)換器

寫(xiě)之前不得提下接口,接口的設(shè)計(jì)是這樣的

@ActivityScope
public class RequestParam<T> {
    public HeaderEntity header;
    public T            body;

    @Inject
    public RequestParam(HeaderEntity header, T body) {
        this.header = header;
        this.params = body;
    }
}

簡(jiǎn)單說(shuō)下,接口請(qǐng)求是application/json;,每個(gè)請(qǐng)求參數(shù)里面包含一個(gè)通用的 header里面有一些api、設(shè)備和token信息,通常這部分是定義到 Http請(qǐng)求頭header里面的,可能我們的接口比較任性...

header里面我們寫(xiě)一個(gè)OkHttp攔截器 然后加header 就可以了,請(qǐng)求體里面攔截器就無(wú)能為力了。所以我們用到了請(qǐng)求轉(zhuǎn)換器。

我們?cè)?code>Retrofit的ApiService把 參數(shù)類(lèi)req 定義為 body,通過(guò)自定義轉(zhuǎn)換器給body加上header組裝成 包含headerbody 的完整對(duì)象param。
以一個(gè)登錄接口為例,說(shuō)明下。
定義在ApiService的方法

    @POST(LOGIN_URL)
    Observable<ResultWrapper<UserLoginResultDto>> login(@Body UserLoginSearchDto dto);
@ActivityScope
public class UserLoginSearchDto {
    public String  account;
    @NeedMD5
    public String  password;//md5 加密
    @Inject
    public UserLoginSearchDto() {}
}
    public void login(BaseActivity activity, UserLoginSearchDto dto, Subscriber<UserLoginResultDto> subscriber) {
        service.login(dto)
               .map(new GetResponseData<>())
               //持久化
               .doOnNext(user -> App.getApp().getAppComponent().userManager().persist(user))
               .compose(activity.bindUntilEvent(ActivityEvent.DESTROY))
               .compose(applySchedulers())
               .subscribe(subscriber);
    }

調(diào)用,可以看到,我們的參數(shù)UserLoginSearchDto并沒(méi)有定義header信息。這時(shí)自定義轉(zhuǎn)換器就上場(chǎng)了。Retrofit 的代理實(shí)現(xiàn)類(lèi)會(huì)通過(guò)請(qǐng)求轉(zhuǎn)化器會(huì)把我們?cè)?code>ApiService定義的@Body UserLoginSearchDto dto序列化為json。

public class RequestConvertFactory extends Converter.Factory {


    public static RequestConvertFactory create() {
        return create(new Gson());
    }

    public static RequestConvertFactory create(Gson gson) {
        return new RequestConvertFactory(gson);
    }

    private final Gson gson;

    private RequestConvertFactory(Gson gson) {
        if (gson == null) throw new NullPointerException("gson == null");
        this.gson = gson;
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(
            Type type,
            Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
//RequestParam 就是包含 header 和 body 的完整參數(shù)類(lèi)
        TypeToken<RequestParam<Type>> typeToken = new TypeToken<RequestParam<Type>>() {};
        TypeAdapter<RequestParam<Type>> adapter = gson.getAdapter(typeToken);
        return new MyGsonRequestBodyConverter<>(gson, adapter);
    }
}
public class MyGsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
    private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
    private static final Charset   UTF_8      = Charset.forName("UTF-8");

    private final Gson                         gson;
    private final TypeAdapter<RequestParam<T>> adapter;

    MyGsonRequestBodyConverter(Gson gson, TypeAdapter<RequestParam<T>> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public RequestBody convert(T value) throws IOException {
        Buffer buffer = new Buffer();
        Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
        JsonWriter jsonWriter = gson.newJsonWriter(writer);
        //head頭參數(shù)
        HeaderEntity header = App.getApp().getAppComponent().header();
        //實(shí)際的參數(shù)實(shí)體
        RequestParam<T> param = new RequestParam<>(header, value);
        //時(shí)間戳 md5
        header.refreshVkey();

        UserManager userManager = App.getApp().getAppComponent().userManager();

        boolean login = value instanceof UserLoginSearchDto;
        header.token = login ? null : userManager.getToken();
        header.userId = login ? 0 : userManager.getUserId();

        //處理 base64
        Class<?> paramClass = value.getClass();
        Field[] fields = paramClass.getFields();
        Field needMD5 = null;
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            NeedMD5 annotation = field.getAnnotation(NeedMD5.class);
            if (annotation != null) {
                needMD5 = field;
                break;
            }
        }
        if (needMD5 != null) {
            try {
                needMD5.set(value, EncryptUtil.encryptMD5(String.valueOf(needMD5.get(value))));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        adapter.write(jsonWriter, param);
        jsonWriter.close();
        //把組裝好的完整 param序列化
        return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
    }
}

別忘了注冊(cè)自定義轉(zhuǎn)換器RequestConvertFactory.create

 Retrofit retrofit = new Retrofit
                .Builder()
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(RequestConvertFactory.create(gson))
                .addConverterFactory(GsonConverterFactory.create(gson))
        ...
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容