Retrofit 進階篇 自定義轉(zhuǎn)換器

1 想必各位android 開發(fā)人員 已經(jīng)對retrofit+rxjava +okhttp 都有所了解 就算是大家沒應(yīng)用 也有所耳聞了 因為本人現(xiàn)在正在寫一個新的工程 正在嘗試著使用 這個很高大上的框架 這這里給大家介紹一下 所遇到的問題 已經(jīng)新知識

基本創(chuàng)建 但是這對于我們一些應(yīng)用 需要參數(shù)加密 數(shù)據(jù)解密的開發(fā)者來講 這樣基本的創(chuàng)建是不足夠的 那么follow me

   retrofit = new Retrofit.Builder()
                    .client(HttpConnection.getInstance())
                    .addConverterFactory(GsonConverterFactory.create())//轉(zhuǎn)換器
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) //添加 RxJava 適配器
                    .baseUrl(Contacts.BASE_URL)
                    .build();

2 .自定義轉(zhuǎn)換器

  • 繼承Converter.Factory
public final class DecodeConverterFactory extends Converter.Factory {

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

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

    private final Gson gson;

    private DecodeConverterFactory(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) {
        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new DecodeRequestBodyConverter<>(gson, adapter);
    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new DecodeResponseBodyConverter<>(adapter);
    }
}
  • 分別實現(xiàn)request,response轉(zhuǎn)換器
public class DecodeRequestBodyConverter<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<T> adapter;
    DecodeRequestBodyConverter(Gson gson,TypeAdapter<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);
        adapter.write(jsonWriter,value);
        jsonWriter.flush();
        byte[] bytes=buffer.readByteArray();
        try{
            bytes=GzipUtil.compress(bytes);
        }catch(Exception e){
            e.printStackTrace();
        }

        bytes=new TripleDES (Contacts.PARAMETER_ENCRYPTION_KEY).encryptionByteData(bytes);
        return RequestBody.create(MEDIA_TYPE,bytes);
    }
}
public class DecodeResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final TypeAdapter<T> adapter;

    DecodeResponseBodyConverter(TypeAdapter<T> adapter) {
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {

        byte[] bytes = value.bytes();
        //先解密 在解壓
        try {
            bytes=  GzipUtil.unZip(new TripleDES(Contacts.BODY_ENCRYPTION_KEY).decryptionByteData(bytes));
        } catch (Exception e) {
            e.printStackTrace();

        }
        LogUtils.d(new String(bytes,"UTF-8"));

        //解密字符串
        return bytes==null?null:adapter.fromJson(new String(bytes,"UTF-8"));
    }
}
  • retrofit的配置


    對就是自定義的轉(zhuǎn)換器的名稱.png
  • 接下來 說說我遇到的問題 我定義了自定義轉(zhuǎn)換器之后 發(fā)現(xiàn) 只是走解密 加密卻怎么都不走 自己一律了半天 才發(fā)現(xiàn) 自己的post 請求 標(biāo)簽 竟然寫成了@ Query 我忍不了自己了 這么二 竟然 這里要很注意的 @Body 這個標(biāo)簽
@POST("check")
    Observable<DataVo<MessageVo,Object>> getTopMovie(@Body EquipmentParam equipment);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 本文將順著構(gòu)建請求對象->構(gòu)建請求接口->發(fā)起同步/異步請求的流程,分析Retrofit是如何實現(xiàn)的。 開始之前,...
    zhuhf閱讀 1,685評論 0 10
  • 介紹 Retrofit 默認提供了gson和Jackson對應(yīng)的converter,但是并沒有提供fastjson...
    我是腿哥閱讀 5,730評論 0 2
  • 簡介 剛接觸Retrofit的時候,就寫了一篇簡單的使用介紹:Retrofit 2.0基本使用方法,算是對Retr...
    Whyn閱讀 3,105評論 4 24
  • 安卓開發(fā)領(lǐng)域中,很多重要的問題都有很好的開源解決方案,例如Square公司提供網(wǎng)絡(luò)請求 OkHttp , Retr...
    aaron688閱讀 1,994評論 1 20
  • 1.1 setNeedsDisplay&& setNeedsLayout介紹 1.2 綜上所述 綜上所訴,setN...
    smkoc閱讀 302評論 0 0

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