package com.jj.network.okhttp;
import com.jj.base.BaseApplication;
import com.jj.util.JJUtils;
import java.io.IOException;
import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* 无网缓存设置,需要在OkHttpClient添加okBuilder.addInterceptor(new NoNetCacheInterceptor());
okBuilder.addNetworkInterceptor(new NetCacheInterceptor());
*/
public class NoNetCacheInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!JJUtils.checkNetworkConnect(BaseApplication.application())) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
return chain.proceed(request).newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, only-if-cached, max-stale=30")
.build();
}
return chain.proceed(request);
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史