use of com.ljy.devring.http.support.interceptor.HttpCacheInterceptor in project DevRing by LJYcoder.
the class RingModule method okHttpClient.
@Singleton
@Provides
OkHttpClient okHttpClient(Application application, OkHttpClient.Builder builder, HttpConfig httpConfig, HttpProgressInterceptor progressInterceptor) {
if (httpConfig.getConnectTimeout() > 0) {
builder.connectTimeout(httpConfig.getConnectTimeout(), TimeUnit.SECONDS);
}
if (httpConfig.isUseLog()) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(loggingInterceptor);
}
if (httpConfig.isUseCache()) {
HttpCacheInterceptor cacheInterceptor = new HttpCacheInterceptor(application, httpConfig.getCacheTimeWithNet(), httpConfig.getCacheTimeWithoutNet());
// 缓存目录
File cacheFile;
if (httpConfig.getCacheFolder() != null && httpConfig.getCacheFolder().isDirectory()) {
cacheFile = httpConfig.getCacheFolder();
} else {
cacheFile = FileUtil.getDirectory(FileUtil.getExternalCacheDir(application), "retrofit_http_cache");
}
// 大小默认20Mb
Cache cache = new Cache(cacheFile, httpConfig.getCacheSize() > 0 ? httpConfig.getCacheSize() : 1024 * 1024 * 20);
builder.addInterceptor(cacheInterceptor);
builder.addNetworkInterceptor(cacheInterceptor);
builder.cache(cache);
}
if (!CollectionUtil.isEmpty(httpConfig.getMapHeader())) {
HttpHeaderInterceptor headerInterceptor = new HttpHeaderInterceptor(httpConfig.getMapHeader());
builder.addInterceptor(headerInterceptor);
}
builder.addNetworkInterceptor(progressInterceptor);
return builder.build();
}
Aggregations