Search code examples
androidkotlinerror-handlingretrofitandroid-jetpack

How can i handle retrofit errors or get url request for to detect problems


I am trying to learn develop android applications and I'm trying to implement the latest approaches. So i use as many jetpack libraries as possible. (Dagger-Hilt, Coroutines, Retrofit etc)

Here is my question:

i have AppModule object for dependency injection.

Here is my retrofit object:

@Singleton 
@Provides 
fun provideConverterApi(): ConverterAPI {
    return Retrofit.Builder()
        .baseUrl(Constants.BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
        .create(ConverterAPI::class.java)
}

How can i get error messages from there or for example i need to see the url i use for the request, how can i do that?


Solution

  • You're doing great, to add a logger for your network call use this way:

    .addConverterFactory(GsonConverterFactory.create())
    .addInterceptor(HttpLoggingInterceptor().apply {
                    level = if (DEBUG) BODY else NONE 
    })
    .build()