0
0
Android Kotlinmobile~10 mins

GET and POST requests 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 create a GET request using OkHttp.

Android Kotlin
val client = OkHttpClient()
val request = Request.Builder()
    .url("https://api.example.com/data")
    .[1]()
    .build()
Drag options to blanks, or click blank then click option'
Apost
Bput
Cget
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using post() instead of get() for fetching data.
Forgetting to call build() after setting the method.
2fill in blank
medium

Complete the code to create a POST request with a JSON body.

Android Kotlin
val JSON = "application/json; charset=utf-8".toMediaType()
val jsonBody = "{\"name\": \"John\"}"
val body = jsonBody.toRequestBody([1])
val request = Request.Builder()
    .url("https://api.example.com/users")
    .post(body)
    .build()
Drag options to blanks, or click blank then click option'
AJSON
Bnull
CMediaType.parse(JSON)
DjsonBody
Attempts:
3 left
💡 Hint
Common Mistakes
Passing null instead of the media type.
Passing the raw JSON string instead of the media type.
3fill in blank
hard

Fix the error in the code to execute the request asynchronously.

Android Kotlin
client.newCall(request).[1](object : Callback {
    override fun onFailure(call: Call, e: IOException) {
        println("Request failed")
    }
    override fun onResponse(call: Call, response: Response) {
        println(response.body?.string())
    }
})
Drag options to blanks, or click blank then click option'
Aenqueue
Bsend
Ccall
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using execute() which blocks the main thread.
Using a non-existent method like send().
4fill in blank
hard

Fill both blanks to create a GET request and print the response body safely.

Android Kotlin
val request = Request.Builder()
    .url("https://api.example.com/info")
    .[1]()
    .build()

client.newCall(request).enqueue(object : Callback {
    override fun onResponse(call: Call, response: Response) {
        val body = response.body?.[2]()
        println(body ?: "No response body")
    }
    override fun onFailure(call: Call, e: IOException) {}
})
Drag options to blanks, or click blank then click option'
Aget
Bpost
Cstring
Dbytes
Attempts:
3 left
💡 Hint
Common Mistakes
Using post() instead of get() for a GET request.
Using bytes() which returns raw bytes instead of a string.
5fill in blank
hard

Fill all three blanks to create a POST request with JSON body and handle the response.

Android Kotlin
val JSON = "application/json; charset=utf-8".toMediaType()
val jsonData = "{\"age\": 30}"
val body = jsonData.toRequestBody([1])
val request = Request.Builder()
    .url("https://api.example.com/update")
    .[2](body)
    .build()

client.newCall(request).enqueue(object : Callback {
    override fun onResponse(call: Call, response: Response) {
        val result = response.body?.[3]()
        println(result ?: "Empty response")
    }
    override fun onFailure(call: Call, e: IOException) {}
})
Drag options to blanks, or click blank then click option'
AJSON
Bpost
Cstring
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using get() instead of post() for sending data.
Passing null or wrong media type to toRequestBody().