42 lines
1.1 KiB
Java
42 lines
1.1 KiB
Java
package com.qxcm.moduleutil.http;
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.TypeAdapter;
|
|
import com.qxcm.moduleutil.utils.GsonUtils;
|
|
|
|
import java.io.IOException;
|
|
|
|
import io.reactivex.annotations.NonNull;
|
|
import okhttp3.ResponseBody;
|
|
import retrofit2.Converter;
|
|
|
|
public class ResponseBodyConverter<T> implements Converter<ResponseBody, T> {
|
|
|
|
private final Gson gson;
|
|
private final TypeAdapter<T> adapter;
|
|
|
|
ResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
|
|
this.gson = gson;
|
|
this.adapter = adapter;
|
|
}
|
|
|
|
@Override
|
|
public T convert(@NonNull ResponseBody value) throws IOException {
|
|
String json = value.string();
|
|
BaseModel obj = GsonUtils.GsonToBean(json, BaseModel.class);
|
|
if (obj.getCode() != 1) {
|
|
String info = obj.getMsg();
|
|
if ("当前余额不足".equals(info)) {
|
|
value.close();
|
|
return adapter.fromJson(json);
|
|
} else {
|
|
throw new APIException(obj.getCode(), info);
|
|
}
|
|
}
|
|
value.close();
|
|
return adapter.fromJson(json);
|
|
}
|
|
|
|
}
|