Reputation: 119
I want to see the logs of Retrofit request body I implemented below code,with below code I am unable o get logs
public static Retrofit getClient(String baseUrl) {
Level logLevel = Level.BODY;
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.level(logLevel);
new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
please help me in this case, Even I allowed logging in developer options,Still is not working
Upvotes: 2
Views: 170
Reputation: 13549
// only instantiate it but not use.
new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
You could do as following:
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new HttpLoggingInterceptor());
Retrofit retrofit = new Retrofit.Builder()
.....
.client(httpClient.build())
.build();
Upvotes: 1
Reputation: 21043
Probably a mistake . You have not set the OkHttpClient
to Retrofit.Builder
this is why its using the default client. Change your code a follows:-
public static Retrofit getClient(String baseUrl) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.level(HttpLoggingInterceptor.Level.BODY);
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.client(new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build())
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
Upvotes: 3