I am using retrofit 2.x and i want to log the header and body of request and response .
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
Oh I found the error if anyone is interested :
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder()
.addHeader("key", "value")
.addHeader("HEADER","HEADER Value")
.build();
return chain.proceed(request);
}
}).build();
You must add the log interceptor (your interceptor variable) after the request interceptor, so the correct answer is:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws
IOException {
Request request = chain.request().newBuilder()
.addHeader("key", "value")
.addHeader("HEADER","HEADER Value")
.build();
return chain.proceed(request);
}
})
.addInterceptor(interceptor)
.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.build();
May it help someone ...
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
Add both to see complete logs and add this interceptor at the last (don't know why but its like this).
You need to set following:
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
clientBuilder.addNetworkInterceptor(httpLoggingInterceptor);
clientBuilder.build()
The following link was very useful: https://medium.com/swlh/okhttp-interceptors-with-retrofit-2dcc322cc3f3
OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
//Gson Builder
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Timber.plant(new Timber.DebugTree());
// HttpLoggingInterceptor
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(message -> Timber.i(message));
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
/**
* injection of interceptors to handle encryption and decryption
*/
//Encryption Interceptor
EncryptionInterceptor encryptionInterceptor = new EncryptionInterceptor(new EncryptionImpl());
//Decryption Interceptor
DecryptionInterceptor decryptionInterceptor = new DecryptionInterceptor(new DecryptionImpl());
// OkHttpClient. Be conscious with the order
OkHttpClient okHttpClient = new OkHttpClient()
.newBuilder()
//httpLogging interceptor for logging network requests
.addInterceptor(httpLoggingInterceptor)
//Encryption interceptor for encryption of request data
.addInterceptor(encryptionInterceptor)
// interceptor for decryption of request data
.addInterceptor(decryptionInterceptor)
.build();
//Retrofit
Retrofit retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(BASE_URL)
// for serialization
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
//ApiService
apiService = retrofit.create(ApiService.class);
Instead of using addInterceptor
to add the logging interceptor, use addNetworkInterceptor
, to include headers added by OkHttp.
Network interceptors are able to:
Observe the data just as it will be transmitted over the network.