Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OkHttp ignore logging interceptor

I faced with strange problem. OkHttp gives me log like this:

I/System.out: [OkHttp] sendRequest>>

I/System.out: [OkHttp] sendRequest<<

But I declarated logging interceptor in Client initialization.

HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);

OkHttpClient.Builder client = new OkHttpClient.Builder()
        .connectTimeout(30, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .addInterceptor(chain -> {
            Request original = chain.request();
            Request.Builder builder = original.newBuilder()
                    .header("Authorization", Credentials.basic(config.login(), config.password()));
            Request request = builder.build();
            return chain.proceed(request);
        })
        .addInterceptor(httpLoggingInterceptor);

return new Retrofit.Builder()
        .baseUrl(config.getServer())
        .callbackExecutor(Executors.newSingleThreadExecutor())
        .addConverterFactory(getMoshiConverterFactory())
        .client(client.build())
        .build();

And my gradle contains:

implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-moshi:2.3.0'
implementation 'com.squareup.moshi:moshi:1.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.7.0'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.7.0'

Am I doing something wrong?

like image 663
Anton A. Avatar asked Oct 23 '25 16:10

Anton A.


2 Answers

You could use it as a network intercepter instead an application interceptor. Do .addNetworkInterceptor(httpLoggingInterceptor); instead of .addInterceptor(httpLoggingInterceptor);

This lets you "Observe the data just as it will be transmitted over the network".

See more information in the wiki pages.

like image 81
Stephan Avatar answered Oct 25 '25 06:10

Stephan


HttpLoggingInterceptor logger = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
        @Override
        public void log(String message) {
            Log.d("OkHttp", message);
        }
    });

Try this way.

like image 36
AphonicLoqui Avatar answered Oct 25 '25 05:10

AphonicLoqui