okhttp Retrofit統一処理返却要求okhttp Retrofit統一処理返却データ
4494 ワード
okhttp Retrofit統一処理返却要求okhttp Retrofit統一処理返却データ
以上の3つのシナリオはRetrofitで処理され、Retrofitとokhttpを結合しなければならない.
もう1つのスキームはokhttpブロッキングで処理することである.接続:https://blog.csdn.net/sun6223508/article/details/101352661
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 responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new ResponseConverter<>(gson, type);
}
/**
*
*/
public class ResponseConverter implements Converter {
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;
}
}
以上の3つのシナリオはRetrofitで処理され、Retrofitとokhttpを結合しなければならない.
もう1つのスキームはokhttpブロッキングで処理することである.接続:https://blog.csdn.net/sun6223508/article/details/101352661