I'm trying to get the exact JSON that is being sent in the request. Here is my code:
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor(){
@Override public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Log.e(String.format("\nrequest:\n%s\nheaders:\n%s",
request.body().toString(), request.headers()));
com.squareup.okhttp.Response response = chain.proceed(request);
return response;
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client).build();
But I only see this in the logs:
request:
com.squareup.okhttp.RequestBody$1@3ff4074d
headers:
Content-Type: application/vnd.ll.event.list+json
How am I supposed to do proper logging, given the removal of setLog()
and setLogLevel()
which we used to use with Retrofit 1?
I found way for Print Log in Retrofit
Works for me.
If you are using Retrofit2 and okhttp3 then you need to know that Interceptor works by queue. So add loggingInterceptor at the end, after your other Interceptors:
Most of the answer here covers almost everything except this tool, one of the coolest ways to see the log.
It is Facebook's Stetho. This is the superb tool to monitor/log your app's network traffic on google chrome. You can also find here on Github.
Try this:
After this, in the
body
there is the JSON you are interested in.Kotlin Code
In Retrofit 2 you should use HttpLoggingInterceptor.
Add dependency to
build.gradle
:Create a
Retrofit
object like the following:The above solution gives you logcat messages very similar to the old ones set by
In case of
java.lang.ClassNotFoundException
:Older Retrofit version might require an older
logging-interceptor
version. Take a look at comments sections for details.