use of okhttp3.internal.cache.CacheInterceptor in project GeekNews by codeestX.
the class HttpModule method provideClient.
@Singleton
@Provides
OkHttpClient provideClient(OkHttpClient.Builder builder) {
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
builder.addInterceptor(loggingInterceptor);
}
File cacheFile = new File(Constants.PATH_CACHE);
Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
Interceptor cacheInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!SystemUtil.isNetworkConnected()) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response response = chain.proceed(request);
if (SystemUtil.isNetworkConnected()) {
int maxAge = 0;
// 有网络时, 不缓存, 最大保存时长为0
response.newBuilder().header("Cache-Control", "public, max-age=" + maxAge).removeHeader("Pragma").build();
} else {
// 无网络时,设置超时为4周
int maxStale = 60 * 60 * 24 * 28;
response.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale).removeHeader("Pragma").build();
}
return response;
}
};
// Interceptor apikey = new Interceptor() {
// @Override
// public Response intercept(Chain chain) throws IOException {
// Request request = chain.request();
// request = request.newBuilder()
// .addHeader("apikey",Constants.KEY_API)
// .build();
// return chain.proceed(request);
// }
// }
// 设置统一的请求头部参数
// builder.addInterceptor(apikey);
//设置缓存
builder.addNetworkInterceptor(cacheInterceptor);
builder.addInterceptor(cacheInterceptor);
builder.cache(cache);
//设置超时
builder.connectTimeout(10, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
//错误重连
builder.retryOnConnectionFailure(true);
return builder.build();
}
use of okhttp3.internal.cache.CacheInterceptor in project Gradle-demo by Arisono.
the class RetrofitImpl method initClient.
@Override
public void initClient() {
// 本类保证初始化一次,减少系统开销
Builder okBuilder = new OkHttpClient.Builder().connectTimeout(mbuilder.getConnectTimeout(), TimeUnit.SECONDS).readTimeout(mbuilder.getReadTimeout(), TimeUnit.SECONDS).writeTimeout(mbuilder.getWriteTimeout(), TimeUnit.SECONDS).sslSocketFactory(OkhttpUtils.createSSLSocketFactory(), // 信任所有证书
new TrustAllCerts()).hostnameVerifier(new TrustAllHostnameVerifier());
LogInterceptor logInterceptor = new LogInterceptor();
logInterceptor.setBuilder(mbuilder);
okBuilder.addInterceptor(logInterceptor);
if (mbuilder.getCacheFileSize() != 0) {
okBuilder.cache(new Cache(mbuilder.getCacheFile(), mbuilder.getCacheFileSize()));
okBuilder.addInterceptor(new CacheInterceptor(String.valueOf(mbuilder.getCacheTime()), mbuilder.getCacheType()));
}
//后期缓存策略改进
switch(mbuilder.getCacheType()) {
case CacheType.ONLY_NETWORK:
OkhttpUtils.println("CacheType.ONLY_NETWORK");
break;
case CacheType.ONLY_CACHED:
OkhttpUtils.println("CacheType.ONLY_CACHED");
break;
case CacheType.CACHED_ELSE_NETWORK:
break;
case CacheType.NETWORK_ELSE_CACHED:
break;
default:
break;
}
OkHttpClient client = okBuilder.build();
retrofit = new Retrofit.Builder().client(client).baseUrl(mbuilder.getBaseUrl()).addConverterFactory(StringConverterFactory.create()).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
}
use of okhttp3.internal.cache.CacheInterceptor in project SeeWeather by xcc3641.
the class RetrofitSingleton method initOkHttp.
private static void initOkHttp() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
// 缓存 http://www.jianshu.com/p/93153b34310e
File cacheFile = new File(C.NET_CACHE);
Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
Interceptor cacheInterceptor = chain -> {
Request request = chain.request();
if (!Util.isNetworkConnected(BaseApplication.getAppContext())) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response response = chain.proceed(request);
Response.Builder newBuilder = response.newBuilder();
if (Util.isNetworkConnected(BaseApplication.getAppContext())) {
int maxAge = 0;
// 有网络时 设置缓存超时时间0个小时
newBuilder.header("Cache-Control", "public, max-age=" + maxAge);
} else {
// 无网络时,设置超时为4周
int maxStale = 60 * 60 * 24 * 28;
newBuilder.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale);
}
return newBuilder.build();
};
builder.cache(cache).addInterceptor(cacheInterceptor);
if (BuildConfig.DEBUG) {
builder.addNetworkInterceptor(new StethoInterceptor());
}
// 设置超时
builder.connectTimeout(15, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
// 错误重连
builder.retryOnConnectionFailure(true);
sOkHttpClient = builder.build();
}
use of okhttp3.internal.cache.CacheInterceptor in project okhttp by square.
the class RealCall method getResponseWithInterceptorChain.
Response getResponseWithInterceptorChain() throws IOException {
// Build a full stack of interceptors.
List<Interceptor> interceptors = new ArrayList<>();
interceptors.addAll(client.interceptors());
interceptors.add(retryAndFollowUpInterceptor);
interceptors.add(new BridgeInterceptor(client.cookieJar()));
interceptors.add(new CacheInterceptor(client.internalCache()));
interceptors.add(new ConnectInterceptor(client));
if (!forWebSocket) {
interceptors.addAll(client.networkInterceptors());
}
interceptors.add(new CallServerInterceptor(forWebSocket));
Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0, originalRequest);
return chain.proceed(originalRequest);
}
use of okhttp3.internal.cache.CacheInterceptor in project bilibili-android-client by HotBitmapGG.
the class RetrofitHelper method initOkHttpClient.
/**
* 初始化OKHttpClient,设置缓存,设置超时时间,设置打印日志,设置UA拦截器
*/
private static void initOkHttpClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
if (mOkHttpClient == null) {
synchronized (RetrofitHelper.class) {
if (mOkHttpClient == null) {
//设置Http缓存
Cache cache = new Cache(new File(BilibiliApp.getInstance().getCacheDir(), "HttpCache"), 1024 * 1024 * 10);
mOkHttpClient = new OkHttpClient.Builder().cache(cache).addInterceptor(interceptor).addNetworkInterceptor(new CacheInterceptor()).addNetworkInterceptor(new StethoInterceptor()).retryOnConnectionFailure(true).connectTimeout(30, TimeUnit.SECONDS).writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).addInterceptor(new UserAgentInterceptor()).build();
}
}
}
}
Aggregations