自定義GsonConverter

通常從服務(wù)端拿到的JSON數(shù)據(jù)格式大概如下:

  {
    "code":1,
    "message":"查詢成功",
    "detail":{"aa":"123","bb":"123","cc":"123"}
  }

如果出現(xiàn)錯(cuò)誤的情況那就是:

  {
    "code":0,
    "message":"密碼錯(cuò)誤!",
    "detail":null
  }

因此通常我們會(huì)定義一個(gè)實(shí)體類來(lái)解析對(duì)應(yīng)的json:

data class BaseResponse<T>(
        var code: Int,
        var message: String,
        var detail: T
)

其中的code字段表示狀態(tài),比如以下值可能代表了不同的含義

  • code = 1, 表示成功
  • code !=1 ,代表錯(cuò)誤
  • 等等等

使用自定義CustomizeGsonConverterFactory

private fun createRetrofit(builder: Retrofit.Builder, client: OkHttpClient, url: String): Retrofit {
        return builder
            .baseUrl(url)
            .client(client)
            //.addConverterFactory(GsonConverterFactory.create())
            .addConverterFactory(CustomizeGsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
    }

addConverterFactory(GsonConverterFactory.create())這句代碼是為了用Gson把服務(wù)端返回的json數(shù)據(jù)解析成實(shí)體的,那就從這里入手,可以自己定義一個(gè)GsonConverter,擴(kuò)展一下原來(lái)的功能,默認(rèn)的GsonConverter由三個(gè)類組成:

  • GsonConverterFactory // GsonConverter 工廠類, 用來(lái)創(chuàng)建GsonConverter
  • GsonResponseBodyConverter // 處理ResponseBody
  • GsonRequestBodyConverter // 處理RequestBody

自定義的GsonConverterFactory

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;

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

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

/**
 * @author lqx
 * 自定義GsonConverterFactory
 */
public class CustomizeGsonConverterFactory extends Converter.Factory {
        public static CustomizeGsonConverterFactory create() {
            return create(new Gson());
        }

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

        private final Gson gson;

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

        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
                                                                Retrofit retrofit) {
            TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
            return new CustomizeGsonResponseBodyConverter<>(gson, adapter);
        }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new CustomizeGsonRequestBodyConverter<>(gson, adapter);
    }

}

自定義的GsonRequestBodyConverter

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;

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

/**
 * @param <T>
 * @author lqx
 */
public class CustomizeGsonRequestBodyConverter<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;

    CustomizeGsonRequestBodyConverter(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.close();
        return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
    }
}

上面的自定義和默認(rèn)的一樣沒(méi)有變化接下來(lái)就是自定義GsonResponseBodyConverter,更改響應(yīng)體中的數(shù)據(jù)

自定義GsonResponseBodyConverter

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.information.screen.BuildConfig;
import com.information.screen.data.http.exception.ApiException;
import com.information.screen.data.http.httpbean.BaseResponse;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * @param <T>
 * @author lqx
 */
public class CustomizeGsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {

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

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

    @Override
    public T convert(ResponseBody value) throws IOException {
        //把responsebody轉(zhuǎn)為string
        String response = value.string();
        if (BuildConfig.DEBUG) {
            //打印后臺(tái)數(shù)據(jù)
            Log.e(BuildConfig.APPLICATION_ID, response);
        }
        BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
        // 這里只是為了檢測(cè)code是否!=1,所以只解析HttpStatus中的字段,因?yàn)橹灰猚ode和message就可以了
        if (baseResponse.getState() != 1) {
            value.close();
            //拋出一個(gè)RuntimeException, 這里拋出的異常會(huì)到subscribe的onError()方法中統(tǒng)一處理
            throw new ApiException(baseResponse.getMsg());
        }
        try {
            return adapter.fromJson(response);
        } finally {
            value.close();
        }
    }
}

最后我們按照正常的RxPresenter邏輯來(lái)處理,寫(xiě)出來(lái)的代碼如下所示:

protected fun <T> requestServiceData(observable: Observable<BaseResponse<T>>, consumer: Consumer<T>) {
        if (checkNetState()) {
            val disposable = observable
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .concatMap(object : Function<BaseResponse<T>, ObservableSource<T>> {
                        override fun apply(t: BaseResponse<T>): ObservableSource<T> {
                            return Observable.just(t.data)
                        }
                    }).subscribe(consumer, mError())
            addSubscribe(disposable)
        } else {
            toastError(Constants.NET_ERROR)
        }
    }

處理錯(cuò)誤

 /**
     * 處理錯(cuò)誤
     */
    private fun mError(): Consumer<Throwable> = Consumer { e ->
        when (e) {
            is SocketTimeoutException -> {
                toastError("網(wǎng)絡(luò)連接超時(shí)")
            }
            is UnknownHostException -> {
                toastError("未知主機(jī)地址")
            }
            is ConnectException -> {
                toastError("網(wǎng)絡(luò)連接異常")
            }
            is JSONException -> {
                toastError("JSONException")
            }
            is ApiException -> {
                toastError(e.msg)
            }
            else -> {
                toastError(e.message.toString())
            }
        }
    }

自定義ApiException

/**
 * 自定義異常信息顯示
 */
data class ApiException(var msg: String) : IOException()
最后編輯于
?著作權(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)容