Search in sources :

Example 1 with PersistentCookieJar

use of com.ljy.devring.http.support.persistentcookiejar.PersistentCookieJar in project DevRing by LJYcoder.

the class RingModule method okHttpClient.

// @Singleton
@Provides
OkHttpClient okHttpClient(Application application, OkHttpClient.Builder builder, HttpConfig httpConfig, HttpLoggingInterceptor loggingInterceptor, HttpCacheInterceptor cacheInterceptor, HttpHeaderInterceptor headerInterceptor, HttpProgressInterceptor progressInterceptor) {
    if (httpConfig.getConnectTimeout() > 0) {
        builder.connectTimeout(httpConfig.getConnectTimeout(), TimeUnit.SECONDS);
    }
    if (httpConfig.getReadTimeout() > 0) {
        builder.readTimeout(httpConfig.getReadTimeout(), TimeUnit.SECONDS);
    }
    if (httpConfig.isUseLog() && !builder.interceptors().contains(loggingInterceptor)) {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);
    }
    if (httpConfig.isUseCache() && !builder.interceptors().contains(cacheInterceptor)) {
        // 缓存目录
        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 (httpConfig.isUseCookie()) {
        if (httpConfig.isCookiePersistent()) {
            ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(application));
            builder.cookieJar(cookieJar);
        } else {
            CookieManager cookieManager = new CookieManager();
            cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
            builder.cookieJar(new JavaNetCookieJar(cookieManager));
        }
    }
    headerInterceptor.setMapHeader(httpConfig.getMapHeader());
    if (!builder.interceptors().contains(headerInterceptor)) {
        builder.addInterceptor(headerInterceptor);
    }
    if (!builder.interceptors().contains(progressInterceptor)) {
        builder.addNetworkInterceptor(progressInterceptor);
    }
    // 配置SSL
    if (httpConfig.getSslSocketFactory() != null && httpConfig.getTrustManager() != null) {
        builder = builder.build().newBuilder().sslSocketFactory(httpConfig.getSslSocketFactory(), httpConfig.getTrustManager());
    }
    return builder.build();
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) ClearableCookieJar(com.ljy.devring.http.support.persistentcookiejar.ClearableCookieJar) PersistentCookieJar(com.ljy.devring.http.support.persistentcookiejar.PersistentCookieJar) SetCookieCache(com.ljy.devring.http.support.persistentcookiejar.cache.SetCookieCache) SharedPrefsCookiePersistor(com.ljy.devring.http.support.persistentcookiejar.persistence.SharedPrefsCookiePersistor) File(java.io.File) CookieManager(java.net.CookieManager) Cache(okhttp3.Cache) DiskCache(com.ljy.devring.cache.support.DiskCache) SpCache(com.ljy.devring.cache.support.SpCache) SetCookieCache(com.ljy.devring.http.support.persistentcookiejar.cache.SetCookieCache) MemoryCache(com.ljy.devring.cache.support.MemoryCache) 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 ClearableCookieJar (com.ljy.devring.http.support.persistentcookiejar.ClearableCookieJar)1 PersistentCookieJar (com.ljy.devring.http.support.persistentcookiejar.PersistentCookieJar)1 SetCookieCache (com.ljy.devring.http.support.persistentcookiejar.cache.SetCookieCache)1 SharedPrefsCookiePersistor (com.ljy.devring.http.support.persistentcookiejar.persistence.SharedPrefsCookiePersistor)1 Provides (dagger.Provides)1 File (java.io.File)1 CookieManager (java.net.CookieManager)1 Cache (okhttp3.Cache)1 JavaNetCookieJar (okhttp3.JavaNetCookieJar)1