Logging With Retrofit 2

Logging with Retrofit 2

In Retrofit 2 you should use HttpLoggingInterceptor.

Add dependency to build.gradle. Latest version as of October 2019 is:

implementation 'com.squareup.okhttp3:logging-interceptor:4.2.1'

Create a Retrofit object like the following:

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://backend.example.com")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();

return retrofit.create(ApiClient.class);

In case of deprecation warnings, simply change setLevel to:

interceptor.level(HttpLoggingInterceptor.Level.BODY);

The above solution gives you logcat messages very similar to the old ones set by

setLogLevel(RestAdapter.LogLevel.FULL)

In case of java.lang.ClassNotFoundException:

Older Retrofit version might require an older logging-interceptor version. Take a look at comments sections for details.

How to log request and response body with Retrofit-Android?

I used setLogLevel(LogLevel.FULL).setLog(new AndroidLog("YOUR_LOG_TAG")), it helped me.

UPDATE.
You can also try for debug purpose use retrofit.client.Response as response model

HttpLoggingInterceptor not logging with retrofit 2

Instead of

val okHttpClient = OkHttpClient.Builder()
.addNetworkInterceptor(interceptor)
...

you should have something like:

val okHttpClient = OkHttpClient.Builder()
.addInterceptor(interceptor)
...

as the addNetworkInterceptor() plays with interceptors that observe a single network request and response, while addInterceptor() adds interceptor that observes the full span of each call: from the connection is established (if any) until after the response source is selected (either the origin server, cache, or both).

EDIT

Errors doing requests on Main Thread are not showing by the logger, so be careful

Doing networking on main thread is not an "ordinary" error. It will result in your app being killed by the system with NetworkOnMainThreadException and this will happen before interceptor would be given any chance to run.

How to add http logging interceptor along with another interceptor in android with kotlin?

Is there any reasons for wanting two different clients?
Seems like you would be fine with using just one and adding both the interceptors to the same client.

This is something in the lines of what it looks like in kotlin.

OkHttpClient.Builder()
.addInterceptor(interceptor)
.addNetworkInterceptor(ApiInterceptor())
.build()
}

Logging with Retrofit2 without OkHttp intercept

You have to add logging-interceptor:

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

compile
'com.squareup.retrofit2:retrofit:2.1.0'

OkHttpClient.Builder builder = new OkHttpClient.Builder();
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.networkInterceptors().add(httpLoggingInterceptor);

OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor).build();

retrofit = new Retrofit.Builder()
.baseUrl(NetworkConstsParkCloud.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();

there are no shortcuts, you can not use retrofit without okhttp, As of 4.4, HttpUrlConnection on Android uses OkHttp under the hood anyway:
https://github.com/google/agera/issues/22



Related Topics



Leave a reply



Submit