通常从服务端拿到的JSON数据格式大概如下:
{
"code":1,
"message":"查询成功",
"detail":{"aa":"123","bb":"123","cc":"123"}
}
如果出现错误的情况那就是:
{
"code":0,
"message":"密码错误!",
"detail":null
}
因此通常我们会定义一个实体类来解析对应的json:
data class BaseResponse<T>(
var code: Int,
var message: String,
var detail: T
)
其中的code字段表示状态,比如以下值可能代表了不同的含义
- code = 1, 表示成功
- code !=1 ,代表错误
- 等等等
使用自定义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把服务端返回的json数据解析成实体的,那就从这里入手,可以自己定义一个GsonConverter
,扩展一下原来的功能,默认的GsonConverter
由三个类组成:
- GsonConverterFactory // GsonConverter 工厂类, 用来创建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());
}
}
上面的自定义和默认的一样没有变化接下来就是自定义GsonResponseBodyConverter,更改响应体中的数据
自定义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转为string
String response = value.string();
if (BuildConfig.DEBUG) {
//打印后台数据
Log.e(BuildConfig.APPLICATION_ID, response);
}
BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
// 这里只是为了检测code是否!=1,所以只解析HttpStatus中的字段,因为只要code和message就可以了
if (baseResponse.getState() != 1) {
value.close();
//抛出一个RuntimeException, 这里抛出的异常会到subscribe的onError()方法中统一处理
throw new ApiException(baseResponse.getMsg());
}
try {
return adapter.fromJson(response);
} finally {
value.close();
}
}
}
最后我们按照正常的RxPresenter逻辑来处理,写出来的代码如下所示:
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)
}
}
处理错误
/**
* 处理错误
*/
private fun mError(): Consumer<Throwable> = Consumer { e ->
when (e) {
is SocketTimeoutException -> {
toastError("网络连接超时")
}
is UnknownHostException -> {
toastError("未知主机地址")
}
is ConnectException -> {
toastError("网络连接异常")
}
is JSONException -> {
toastError("JSONException")
}
is ApiException -> {
toastError(e.msg)
}
else -> {
toastError(e.message.toString())
}
}
}
自定义ApiException
/**
* 自定义异常信息显示
*/
data class ApiException(var msg: String) : IOException()