0
0
Android Kotlinmobile~10 mins

OkHttp interceptors in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a logging interceptor to the OkHttpClient builder.

Android Kotlin
val client = OkHttpClient.Builder()
    .addInterceptor({ chain ->
        val request = chain.request()
        println("Request URL: ${request.url}")
        chain.proceed(request)
    })
    .[1]()
Drag options to blanks, or click blank then click option'
Abuild
Brun
Cexecute
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using start() or execute() instead of build()
Forgetting to call any method after adding the interceptor
2fill in blank
medium

Complete the code to create an interceptor that adds a custom header "X-Auth" with value "token123".

Android Kotlin
val interceptor = Interceptor { chain ->
    val newRequest = chain.request().newBuilder()
        .[1]("X-Auth", "token123")
        .build()
    chain.proceed(newRequest)
}
Drag options to blanks, or click blank then click option'
AsetHeader
BaddHeader
CremoveHeader
Dheader
Attempts:
3 left
💡 Hint
Common Mistakes
Using setHeader which does not exist
Using header which is a getter, not a setter
3fill in blank
hard

Fix the error in the interceptor code to correctly log the response body as a string.

Android Kotlin
val loggingInterceptor = Interceptor { chain ->
    val response = chain.proceed(chain.request())
    val bodyString = response.body?.string() ?: ""
    println("Response: ${bodyString}")
    [1]
}
Drag options to blanks, or click blank then click option'
Aresponse.newBuilder().body(response.body).build()
Bresponse
Cresponse.newBuilder().body(ResponseBody.create(null, bodyString)).build()
Dresponse.newBuilder().body(ResponseBody.create(response.body?.contentType(), bodyString)).build()
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the original response after consuming the body causes errors
Using null content type loses the original content type
4fill in blank
hard

Fill both blanks to create an interceptor that retries the request once if the response code is 500.

Android Kotlin
val retryInterceptor = Interceptor { chain ->
    var response = chain.proceed(chain.request())
    if (response.code == [1]) {
        response.close()
        response = chain.proceed(chain.request())
    }
    response
}
Drag options to blanks, or click blank then click option'
A500
B404
C200
D503
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 which means not found
Not closing the original response before retrying
5fill in blank
hard

Fill all three blanks to create a custom interceptor that adds a query parameter "lang" with value "en" to every request URL.

Android Kotlin
val langInterceptor = Interceptor { chain ->
    val originalRequest = chain.request()
    val originalUrl = originalRequest.url
    val newUrl = originalUrl.newBuilder()
        .[1]("lang", "en")
        .build()
    val newRequest = originalRequest.newBuilder()
        .[2](newUrl)
        .build()
    chain.[3](newRequest)
}
Drag options to blanks, or click blank then click option'
AaddQueryParameter
Burl
Cproceed
DaddHeader
Attempts:
3 left
💡 Hint
Common Mistakes
Using addHeader instead of addQueryParameter
Not updating the request URL properly
Calling chain.proceed with the original request