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
Difference Between Declaring Variables Before or in Loop
How to Establish a Connection Pool in Jdbc
Something Seems Wrong With the Layout, Jbutton Showing Unexpected Behaviour At Resize of the Window
How to Turn a List of Lists into a List in Java 8
Get a List of All Threads Currently Running in Java
How to Loop Through a Hashmap in Jsp
What's Causing My Java.Net.Socketexception: Connection Reset
Sort on a String That May Contain a Number
Replacements For Deprecated Jpms Modules With Java Ee APIs
What's a Good Java, Curses-Like, Library For Terminal Applications
Is There a Difference Between X++ and ++X in Java
What Is the Purpose of the Expression "New String(...)" in Java
Loading and Displaying Large Text Files
Removing Whitespace from Strings in Java
Why Do I Get an Unsupportedoperationexception When Trying to Remove an Element from a List
What Does the ^ Operator Do in Java