Search in sources :

Example 96 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project drift-sdk-android by Driftt.

the class APIManager method setupRestClient.

private static void setupRestClient() {
    Gson gson = generateGson();
    // HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    // if (!BuildConfig.DEBUG) {
    // loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
    // } else {
    // loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    // }
    OkHttpClient customerClient = new OkHttpClient.Builder().addInterceptor(new APIAuthTokenInterceptor()).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(API_CUSTOMER_URL).callFactory(customerClient).addConverterFactory(GsonConverterFactory.create(gson)).build();
    REST_CUSTOMER_CLIENT = retrofit.create(APICustomerAPIBuilder.class);
    OkHttpClient conversationClient = new OkHttpClient.Builder().addInterceptor(new APIAuthTokenInterceptor()).build();
    Retrofit retrofitV2 = new Retrofit.Builder().baseUrl(API_CONVERSATION_URL).callFactory(conversationClient).addConverterFactory(GsonConverterFactory.create(gson)).build();
    REST_CONVERSATION_CLIENT = retrofitV2.create(APIConversationAPIBuilder.class);
    OkHttpClient authlessClient = new OkHttpClient.Builder().build();
    Retrofit retrofitV3 = new Retrofit.Builder().baseUrl(API_CONVERSATION_URL).callFactory(authlessClient).addConverterFactory(GsonConverterFactory.create(gson)).build();
    REST_AUTHLESS_CLIENT = retrofitV3.create(APIAuthlessBuilder.class);
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson)

Example 97 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project Audient by komamj.

the class AudientRepositoryModule method provideOkHttpClient.

@Singleton
@Provides
OkHttpClient provideOkHttpClient(Cache cache, final SharedPreferences sharedPreferences, final Gson gson) {
    final HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
    if (BuildConfig.DEBUG) {
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    }
    return new OkHttpClient.Builder().authenticator(new Authenticator() {

        @Nullable
        @Override
        public Request authenticate(Route route, Response response) throws IOException {
            String refershToken = sharedPreferences.getString(Constants.REFRESH_TOKEN, "");
            Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.AUDIENT_HOST).client(new OkHttpClient.Builder().addInterceptor(logInterceptor).build()).addConverterFactory(GsonConverterFactory.create(gson)).addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build();
            AudientApi audientApi = retrofit.create(AudientApi.class);
            audientApi.refreshAccessToken(GRANT_TYPE, refershToken, Constants.CLIENT_ID, Constants.CLIENT_SECRET).subscribeWith(new DisposableSubscriber<Token>() {

                @Override
                public void onNext(Token token) {
                    sharedPreferences.edit().putString(Constants.ACCESS_TOKEN, token.accessToken).commit();
                    sharedPreferences.edit().putString(Constants.REFRESH_TOKEN, token.refreshToken).commit();
                }

                @Override
                public void onError(Throwable t) {
                    LogUtils.e(TAG, "refresh token error :" + t.getMessage());
                    sharedPreferences.edit().putBoolean(Constants.LOGIN_STATUS, false).commit();
                    android.os.Process.killProcess(android.os.Process.myPid());
                }

                @Override
                public void onComplete() {
                }
            });
            return response.request().newBuilder().header("Authorization", "Bearer " + sharedPreferences.getString(Constants.ACCESS_TOKEN, "")).build();
        }
    }).addInterceptor(new TokenInterceptor(sharedPreferences)).addInterceptor(logInterceptor).cache(cache).connectTimeout(15, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).writeTimeout(20, TimeUnit.SECONDS).retryOnConnectionFailure(true).build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) Request(okhttp3.Request) Token(com.xinshang.audient.model.entities.Token) IOException(java.io.IOException) DisposableSubscriber(io.reactivex.subscribers.DisposableSubscriber) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Authenticator(okhttp3.Authenticator) Nullable(android.support.annotation.Nullable) Route(okhttp3.Route) TokenInterceptor(com.xinshang.audient.model.helper.TokenInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 98 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project android-bootstrap by sahiljain5040.

the class NetworkModule method provideOkHttpClient.

@Provides
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
    Log.d(TAG, "provideOkHttpClientNectors called");
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder builder = new OkHttpClient.Builder().cache(cache);
    builder.networkInterceptors().add(interceptor);
    OkHttpClient client = builder.build();
    return client;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 99 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project AndroidQuick by ddnosh.

the class RetrofitManager method initOkHttp.

private void initOkHttp() {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    if (BuildConfig.DEBUG) {
        // Log信息拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        // 这里可以选择拦截级别
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        // 设置 Debug Log 模式
        builder.addInterceptor(loggingInterceptor);
        // 配置SSL证书检测
        builder.sslSocketFactory(SSLSocketClient.getNoSSLSocketFactory());
        builder.hostnameVerifier(SSLSocketClient.getHostnameVerifier());
    }
    // 错误重连
    builder.retryOnConnectionFailure(true);
    okHttpClient = builder.build();
    LogUtil.i(TAG, "initOkHttp:getNoSSLSocketFactory");
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 100 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project MovieGuide by esoxjem.

the class NetworkModule method provideOkHttpClient.

@Provides
@Singleton
OkHttpClient provideOkHttpClient(RequestInterceptor requestInterceptor) {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.connectTimeout(CONNECT_TIMEOUT_IN_MS, TimeUnit.MILLISECONDS).addInterceptor(requestInterceptor);
    if (BuildConfig.DEBUG) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);
    }
    return builder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

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