Challenge - 5 Problems
Retrofit Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Correct Retrofit Builder Syntax
Which option correctly creates a Retrofit instance with base URL "https://api.example.com/" and Gson converter?
Attempts:
2 left
💡 Hint
Remember to call the create() method with parentheses and include the trailing slash in the base URL.
✗ Incorrect
Option D correctly calls GsonConverterFactory.create() with parentheses and includes the trailing slash in the base URL. Option D misses parentheses on create(), Option D misses the .create() call and trailing slash, and Option D misses parentheses on build().
❓ ui_behavior
intermediate2:00remaining
Retrofit Network Call Behavior
What happens when you call a Retrofit service method that returns Call and you execute it asynchronously with enqueue()?
Attempts:
2 left
💡 Hint
Think about how Retrofit handles threading for asynchronous calls.
✗ Incorrect
Retrofit's enqueue() method runs the network call on a background thread and delivers the callback results on the main thread, so UI updates can be done safely.
❓ lifecycle
advanced2:00remaining
Handling Retrofit Call Cancellation
Which option correctly cancels an ongoing Retrofit Call to avoid memory leaks when an Android Activity is destroyed?
Android Kotlin
var call: Call<ResponseBody>? = null
fun fetchData() {
call = retrofitService.getData()
call?.enqueue(object : Callback<ResponseBody> {
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
// handle response
}
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
// handle failure
}
})
}
fun onDestroy() {
// What to do here?
}Attempts:
2 left
💡 Hint
To stop a network call, you must explicitly cancel it.
✗ Incorrect
Calling cancel() on the Call object stops the network request and prevents callbacks from being invoked, avoiding leaks. Setting call to null does not stop the request. clone() creates a new call. enqueue(null) is invalid.
advanced
2:00remaining
Retrofit and Navigation Timing
You want to navigate to a new screen only after a successful Retrofit network response. Which approach ensures navigation happens at the right time?
Attempts:
2 left
💡 Hint
When does Retrofit notify you of a successful response?
✗ Incorrect
Navigation should happen inside onResponse after confirming the response is successful. Calling navigation immediately after enqueue() runs before the network call finishes. onFailure means the call failed. Before enqueue() means no network call started yet.
🧠 Conceptual
expert2:00remaining
Retrofit Converter Factory Role
What is the main role of a Converter Factory like GsonConverterFactory in Retrofit setup?
Attempts:
2 left
💡 Hint
Think about how Retrofit transforms raw data into usable objects.
✗ Incorrect
Converter factories like GsonConverterFactory parse JSON strings into Kotlin/Java objects and convert objects into JSON for sending requests. They do not manage retries, threading, or caching.