Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant resolve setLevel on HttpLoggingInterceptor Retrofit2.0

I am using Retrofit and I want to log my response and other things, I am using this https://futurestud.io/tutorials/retrofit-2-log-requests-and-responses type but I am facing Cant resolve setLevel error

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();  

logging.setLevel(Level.BODY); // i am getting error on this

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();  

httpClient.addInterceptor(logging); //this is also getting error

I am using this compile 'com.squareup.okhttp3:logging-interceptor:3.3.1' and Retrofit compile 'com.squareup.retrofit2:converter-gson:2.1.0' dependency.

This is where i got error

like image 981
Mohit Suthar Avatar asked Sep 30 '16 05:09

Mohit Suthar


People also ask

How do I log into a retrofit URL?

Logging In Retrofit 2HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); // set your desired log level logging. setLevel(Level. BODY); OkHttpClient. Builder httpClient = new OkHttpClient.

What is HttpLoggingInterceptor?

Class HttpLoggingInterceptorAn OkHttp interceptor which logs request and response information.


1 Answers

Add this dependency to your app's gradle:

 compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'

And use it like below: Build your OkHttpClient like below:

final OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClientBuilder.addInterceptor(interceptor);
okHttpClientBuilder.connectTimeout(RestConstants.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
return okHttpClientBuilder.build();

And set it to your Retrofit as Client.

I recommend you to check if your app is Debuggable and than set your log interceptor. So in production you'll not log api results.

like image 122
savepopulation Avatar answered Oct 16 '22 02:10

savepopulation