How can I debug my retrofit API call?
Use HttpLoggingInterceptor along with Retrofit.
If this helps, add this inside your build.gradle -
//Retrofit and OkHttp for Networkingimplementation 'com.squareup.retrofit2:retrofit:2.9.0'implementation 'com.squareup.retrofit2:converter-gson:2.9.0'//Logging Network Callsimplementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
Inside your APIClient class
add this -
public class ApiClient { private static Retrofit retrofit = null; public static Retrofit getClient(){ HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(interceptor) .build(); if(retrofit==null){ retrofit = new Retrofit.Builder() .baseUrl(BuildConfig.baseUrl) .addConverterFactory(GsonConverterFactory.create()) .client(client) .build(); } return retrofit; }}
Kotlin Code
val interceptor : HttpLoggingInterceptor = HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY }val client : OkHttpClient = OkHttpClient.Builder().apply { addInterceptor(interceptor) }.build()fun getService(): Service { return Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(LiveDataCallAdapterFactory()) .client(client) .build() .create(Service::class.java) }
And you will be able to log the Retrofit Network calls that you make.
Let me know if you need more information.
An OkHttp interceptor which logs HTTP request and response data.
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();logging.setLevel(Level.BASIC);OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(logging) .build();
You can change the log level at any time by calling setLevel
.
There are 4 levels: NONE, BASIC, HEADERS, BODY
To log to a custom location, pass a Logger
instance to the constructor.
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new Logger() {@Override public void log(String message) { Log.d(TAG, "message: "); }});
From Gradle
compile 'com.squareup.okhttp3:logging-interceptor:(insert latest version)'
Follow this reference
EDITED: I also found this library which has a very nice structure and clean log. Try it!!
You can use the following class to log API calls
import okhttp3.OkHttpClientimport okhttp3.logging.HttpLoggingInterceptorobject HTTPLogger { fun getLogger(): OkHttpClient { /* * OKHTTP interceptor to log all API calls */ val interceptor = HttpLoggingInterceptor() interceptor.level = HttpLoggingInterceptor.Level.BODY val client = OkHttpClient.Builder() .addInterceptor(interceptor) .build() return client }}
You can then call this class in your retrofit instance class like this
import retrofit2.Retrofitimport retrofit2.adapter.rxjava2.RxJava2CallAdapterFactoryimport retrofit2.converter.gson.GsonConverterFactoryobject RetrofitClientInstance { private var retrofit: Retrofit? = null val retrofitInstance: Retrofit? get() { if (retrofit == null) { retrofit = Retrofit.Builder() .baseUrl(Constants.BASE_URL) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .client(HTTPLogger.getLogger()) .build() } return retrofit }}
The dependency required is
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'