okhttp Retrofit统一处理返回请求 okhttp Retrofit统一处理返回数据
Gson gson = new GsonBuilder()
.serializeNulls()
.create();
//1.配置创建okhttp客户端
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder()
.addInterceptor(getLogInterceptor())//
.addInterceptor(getTimeIntercepter())
.connectTimeout(CONNECT_TIME_OUT, TimeUnit.MILLISECONDS) //与服务器连接超时时
间
.readTimeout(READ_TIME_OUT, TimeUnit.MILLISECONDS)
.writeTimeout(WRITE_TIME_OUT, TimeUnit.MILLISECONDS)
.retryOnConnectionFailure(true)//路由等失败自动重连
.sslSocketFactory(sslSocketFactory)//https 绕过验证
.hostnameVerifier(hostnameVerifier)
//2.创建
Retrofit retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(baseUrl)
.addConverterFactory(ConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
/**
*自定义的转换器工厂类
*/
public class ConverterFactory extends Converter.Factory {
private final Gson gson;
public static ConverterFactory create() {
return create(new Gson());
}
public static ConverterFactory create(Gson gson) {
return new ConverterFactory(gson);
}
private ConverterFactory(Gson gson) {
if (gson == null) throw new NullPointerException(ConverterFactory.class.getSimpleName()+":gson == null");
this.gson = gson;
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new ResponseConverter<>(gson, type);
}
/**
* 自定义响应体的转换器
*/
public class ResponseConverter<T> implements Converter<ResponseBody, T> {
public static final String TAG = "ResponseConverter";
private final Gson gson;
private final Type type;
ResponseConverter(Gson gson, Type type) {
this.gson = gson;
this.type = type;
}
@Override
public T convert(final ResponseBody value) throws IOException {
ResBase base = null;
String response = null;
try {
response = value.string();
} catch (IOException e) {
e.printStackTrace();
TagLibUtil.showLogDebug("请求成功,获取返回值失败");
return null;
}
Log.i("thread",Thread.currentThread().getName()) ;
try {
base = gson.fromJson(response, type);
if (base != null&& !InvalidUtil.checkSign(base.sign, response) && ApiBox.getInstance().isVeriNgis()) {//校验错误
throw new InvalidException(InvalidException.FLAG_ERROR_RESPONCE_CHECK,base.msg,base);
}
Major.duff = System.currentTimeMillis() - base.ts;
if(base != null&&base._pdakey!=null&&base._pdakey.equals("")){
ApiBox.REQUEST_KEY = base._pdakey;
ApiBox.getInstance().resetPreferencesReqkey(ApiBox.getInstance().application,base._pdakey);
}
if (base != null && base.state != 1) {//非成功
if (base != null && base.state == 0 && !TextUtils.isEmpty(base.msg) &&
(!TextUtils.isEmpty(base.msg) && base.msg.contains("0x04") || !TextUtils.isEmpty(base.msg) && base.msg.contains("0x02"))) {
TagLibUtil.showLogDebug("系统级错误 message出现");
//重新登录
throw new InvalidException(InvalidException.FLAG_ERROR_RELOGIN,base.msg,base);
} else {//失败 -- 订阅者 自己在onNext做处理逻辑
// if(base==null&&!TextUtils.isEmpty(response)){
// base.jsonStr=response;
// }
// throw new UserException(base.code, base.msg,base);
}
}
} finally {
value.close();
}
// if(base!=null&&!TextUtils.isEmpty(response)){
// base.jsonStr=response;
// }
return (T) base;
}
}
以上三种方案 是在 Retrofit 处理,必须Retrofit和okhttp结合
还有另外一种方案是在okhttp 拦截器 处理。连接:https://blog.csdn.net/sun6223508/article/details/101352661