OkHttp Interceptors in Android

May 25 2021 · Kotlin 1.4, Android 5, Android Studio 4.1

Part 1: Implementing OkHttp Interceptors

03. Setup HttpLoggingInterceptor & Debug API Calls

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 02. Set Up the Project Next episode: 04. Implement a Custom Logger & Redact Headers

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

HttpLoggingInterceptor is an Interceptor that lets you log all the API requests made by your app as well as the responses returned by the server.

implementation “com.squareup.okhttp3:logging-interceptor:4.9.0”`
fun getOkHttpClient(): OkHttpClient {
    return if (okHttpClient == null) {
        val okHttpClient = OkHttpClient.Builder()
            .readTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
            .connectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
            .build()
        OkHttpProvider.okHttpClient = okHttpClient
        okHttpClient
    } else {
        okHttpClient!!
    }
}
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC
val okHttpClient = OkHttpClient.Builder()
    .readTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
    .connectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
    .addInterceptor(loggingInterceptor)
    .build()
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
@GET("/3/movie/{id}")
fun getMovieDetails(@Path("id") id: Long, @Query("api_key") api_key: String = BuildConfig.THE_MOVIE_DB_API_TOKEN): Call<MovieDetailsModel>