Search in sources :

Example 66 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor 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)

Example 67 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project loc-framework by lord-of-code.

the class OkHttpLoggingAutoConfiguration method okHttp3LoggingInterceptor.

@Bean
@ConditionalOnMissingBean
public HttpLoggingInterceptor okHttp3LoggingInterceptor(OkHttpClientProperties loggingProperties) {
    HttpLoggingInterceptor httpLoggingInterceptor = Optional.ofNullable(logger).map(l -> new HttpLoggingInterceptor(logger)).orElseGet(HttpLoggingInterceptor::new);
    Optional.ofNullable(loggingProperties.getLevel()).ifPresent(httpLoggingInterceptor::setLevel);
    return httpLoggingInterceptor;
}
Also used : Configuration(org.springframework.context.annotation.Configuration) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ObjectProvider(org.springframework.beans.factory.ObjectProvider) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) Optional(java.util.Optional) Bean(org.springframework.context.annotation.Bean) AutoConfigureBefore(org.springframework.boot.autoconfigure.AutoConfigureBefore) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 68 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project Api-Startup-Android by salyangoz.

the class ApiClientProvider method getInstance.

public static ApiClient getInstance() {
    if (apiClient == null) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        httpClient.addInterceptor(loggingInterceptor);
        httpClient.addInterceptor(new HeaderInterceptor());
    }
    OAuthResponse oAuthResponse = OAuthResponse.get();
    if (oAuthResponse != null && authInterceptor == null) {
        authInterceptor = new AuthenticationInterceptor(oAuthResponse.getAccessToken());
        // Add Bearer Authentication if Authentication Token Exists
        if (!httpClient.interceptors().contains(authInterceptor)) {
            httpClient.authenticator(new TokenAuthenticator());
            httpClient.interceptors().add(authInterceptor);
        }
    }
    builder.client(httpClient.build()).build();
    retrofit = builder.build();
    retrofit.responseBodyConverter(APIError.class, new Annotation[0]);
    return retrofit.create(ApiClient.class);
}
Also used : OAuthResponse(io.salyangoz.apistartup.model.response.OAuthResponse) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 69 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project Ency by xiarunhao123.

the class HttpModule method provideOkHttpClient.

@Provides
@Singleton
OkHttpClient provideOkHttpClient(OkHttpClient.Builder builder, final Context context) {
    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 (!AppNetWorkUtil.isNetworkConnected(context)) {
                request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
            }
            Response response = chain.proceed(request);
            if (AppNetWorkUtil.isNetworkConnected(context)) {
                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);
    return builder.build();
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) File(java.io.File) Interceptor(okhttp3.Interceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 70 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project apm-agent-java by elastic.

the class ReporterFactory method getOkHttpClient.

@Nonnull
OkHttpClient getOkHttpClient(ReporterConfiguration reporterConfiguration) {
    final OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(reporterConfiguration.getServerTimeout(), TimeUnit.SECONDS);
    if (!reporterConfiguration.isVerifyServerCert()) {
        disableCertificateValidation(builder);
    }
    if (logger.isTraceEnabled()) {
        final HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        builder.addInterceptor(loggingInterceptor);
    }
    return builder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Nonnull(javax.annotation.Nonnull)

Aggregations

HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)107 OkHttpClient (okhttp3.OkHttpClient)87 Provides (dagger.Provides)27 Retrofit (retrofit2.Retrofit)25 GsonBuilder (com.google.gson.GsonBuilder)24 Singleton (javax.inject.Singleton)24 Cache (okhttp3.Cache)19 Request (okhttp3.Request)16 File (java.io.File)15 Response (okhttp3.Response)15 IOException (java.io.IOException)14 Gson (com.google.gson.Gson)13 Interceptor (okhttp3.Interceptor)11 StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)6 LoggingInterceptor (com.toshi.manager.network.interceptor.LoggingInterceptor)6 List (java.util.List)5 ErrorInterceptor (me.postaddict.instagram.scraper.interceptor.ErrorInterceptor)5 FakeBrowserInterceptor (me.postaddict.instagram.scraper.interceptor.FakeBrowserInterceptor)5 ResponseBody (okhttp3.ResponseBody)5 BeforeClass (org.junit.BeforeClass)5