Search in sources :

Example 1 with CookiesManager

use of json.chao.com.wanandroid.core.http.cookies.CookiesManager in project Awesome-WanAndroid by JsonChao.

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 = chain -> {
        Request request = chain.request();
        if (!CommonUtils.isNetworkConnected()) {
            request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
        }
        Response response = chain.proceed(request);
        if (CommonUtils.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;
    };
    // 设置缓存
    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);
    // cookie认证
    builder.cookieJar(new CookiesManager());
    return builder.build();
}
Also used : Interceptor(okhttp3.Interceptor) Request(okhttp3.Request) Cache(okhttp3.Cache) GeeksApis(json.chao.com.wanandroid.core.http.api.GeeksApis) CacheControl(okhttp3.CacheControl) Singleton(javax.inject.Singleton) File(java.io.File) WanAndroidUrl(json.chao.com.wanandroid.di.qualifier.WanAndroidUrl) Retrofit(retrofit2.Retrofit) RxJava2CallAdapterFactory(retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory) CommonUtils(json.chao.com.wanandroid.utils.CommonUtils) TimeUnit(java.util.concurrent.TimeUnit) Constants(json.chao.com.wanandroid.app.Constants) Module(dagger.Module) OkHttpClient(okhttp3.OkHttpClient) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) BuildConfig(json.chao.com.wanandroid.BuildConfig) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) Response(okhttp3.Response) CookiesManager(json.chao.com.wanandroid.core.http.cookies.CookiesManager) Provides(dagger.Provides) Response(okhttp3.Response) Request(okhttp3.Request) File(java.io.File) Interceptor(okhttp3.Interceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache) CookiesManager(json.chao.com.wanandroid.core.http.cookies.CookiesManager) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

Module (dagger.Module)1 Provides (dagger.Provides)1 File (java.io.File)1 TimeUnit (java.util.concurrent.TimeUnit)1 Singleton (javax.inject.Singleton)1 BuildConfig (json.chao.com.wanandroid.BuildConfig)1 Constants (json.chao.com.wanandroid.app.Constants)1 GeeksApis (json.chao.com.wanandroid.core.http.api.GeeksApis)1 CookiesManager (json.chao.com.wanandroid.core.http.cookies.CookiesManager)1 WanAndroidUrl (json.chao.com.wanandroid.di.qualifier.WanAndroidUrl)1 CommonUtils (json.chao.com.wanandroid.utils.CommonUtils)1 Cache (okhttp3.Cache)1 CacheControl (okhttp3.CacheControl)1 Interceptor (okhttp3.Interceptor)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)1 Retrofit (retrofit2.Retrofit)1 RxJava2CallAdapterFactory (retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory)1