@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
//缓存
if(NetUtil.checkNetwork(mContext)==NetUtil.NO_NETWORK){
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
Log.d("zgx","no network");
}
okhttp3.Response response = chain.proceed(request);
String requestUrl = response.request().url().uri().getPath();
if(!TextUtils.isEmpty(requestUrl)){
if(requestUrl.contains("LoginDataServlet")) {
if (Looper.myLooper() == null) {
Looper.prepare();
}
createObservable("现在请求的是登录接口");
}
}
//缓存响应
if(NetUtil.checkNetwork(mContext)!=NetUtil.NO_NETWORK){
//有网的时候读接口上的@Headers里的配置,你可以在这里进行统一的设置
String cacheControl = request.cacheControl().toString();
Log.e("zgx","request====="+request);
Log.e("zgx","request.toString()====="+request.header("body"));
Log.e("zgx","request.cacheControl()====="+request.cacheControl());
Log.e("zgx","cacheControl====="+cacheControl);
return response.newBuilder()
.header("Cache-Control", cacheControl)
.removeHeader("Pragma")
.build();
}else{
return response.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=120")
.removeHeader("Pragma")
.build();
}
}
为什么String cacheControl = request.cacheControl().toString();获取不到值呢?