Search code examples
androidloggingretrofit2okhttp

Android Retrofit log does not show


Retrofit does not print log. Log level set to verbose. Tried HttpLoggingInterceptor too.

using these gradles

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'

Code

OkHttpClient httpClient = new OkHttpClient();
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.newBuilder().connectTimeout(6, TimeUnit.MINUTES)
                .readTimeout(6, TimeUnit.MINUTES)
                .writeTimeout(6, TimeUnit.MINUTES)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request original = chain.request();
                        Request.Builder requestBuilder = original.newBuilder()
                                .header("Content-Type", "application/json")
                                .header("Authorization", "aDRF@F#JG_a34-n3d")
                                .method(original.method(), original.body());
                        Request request = requestBuilder.build();
                        return chain.proceed(request);
                    }
                })
    .addInterceptor(httpLoggingInterceptor);


return httpClient;

Solution

  • In fact I don't know why you don't see logs, but here is part of my code which works fine:

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
    httpClientBuilder.addInterceptor(logging);
    
    String creditentials = username + ":" + password;
    _base64 = "Basic " + Base64.encodeToString(creditentials.getBytes(), Base64.NO_WRAP);
    
    Interceptor authorization = chain -> {
        Request newRequest = chain.request().newBuilder()
                .addHeader("Authorization", _base64)
                .addHeader("Accept", "application/json")
                .build();
        return chain.proceed(newRequest);
    };
    
    httpClientBuilder.addInterceptor(authorization);
    
    _retrofit = new Retrofit.Builder()
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(API_URL)
            .client(httpClientBuilder.build())
            .build();