Search in sources :

Example 1 with HttpCacheInterceptor

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();
}
Also used : HttpHeaderInterceptor(com.ljy.devring.http.support.interceptor.HttpHeaderInterceptor) HttpCacheInterceptor(com.ljy.devring.http.support.interceptor.HttpCacheInterceptor) File(java.io.File) HttpLoggingInterceptor(com.ljy.devring.http.support.interceptor.HttpLoggingInterceptor) Cache(okhttp3.Cache) DiskCache(com.ljy.devring.cache.support.DiskCache) SpCache(com.ljy.devring.cache.support.SpCache) MemoryCache(com.ljy.devring.cache.support.MemoryCache) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

DiskCache (com.ljy.devring.cache.support.DiskCache)1 MemoryCache (com.ljy.devring.cache.support.MemoryCache)1 SpCache (com.ljy.devring.cache.support.SpCache)1 HttpCacheInterceptor (com.ljy.devring.http.support.interceptor.HttpCacheInterceptor)1 HttpHeaderInterceptor (com.ljy.devring.http.support.interceptor.HttpHeaderInterceptor)1 HttpLoggingInterceptor (com.ljy.devring.http.support.interceptor.HttpLoggingInterceptor)1 Provides (dagger.Provides)1 File (java.io.File)1 Singleton (javax.inject.Singleton)1 Cache (okhttp3.Cache)1