Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using start() or execute() instead of build()
Forgetting to call any method after adding the interceptor
✗ Incorrect
The OkHttpClient.Builder requires calling build() to create the OkHttpClient instance.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using setHeader which does not exist
Using header which is a getter, not a setter
✗ Incorrect
To add a header in OkHttp request builder, use addHeader().
3fill in blank
hardFix 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'
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
✗ Incorrect
Reading response.body.string() consumes the body, so we must create a new response with a new body containing the same content.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 which means not found
Not closing the original response before retrying
✗ Incorrect
HTTP status code 500 means server error; retrying on 500 is common.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using addHeader instead of addQueryParameter
Not updating the request URL properly
Calling chain.proceed with the original request
✗ Incorrect
To add a query parameter, use addQueryParameter on the URL builder, set the new URL with url(), and proceed with the new request.