Challenge - 5 Problems
Network Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when this GET request code runs?
Consider this Kotlin code snippet in an Android app making a GET request. What will be the output in the log if the server responds with status 200 and body "Hello World"?
Android Kotlin
val url = URL("https://example.com/api/greet") val connection = url.openConnection() as HttpURLConnection connection.requestMethod = "GET" val responseCode = connection.responseCode val response = connection.inputStream.bufferedReader().readText() Log.d("HTTP", "Code: $responseCode, Body: $response")
Attempts:
2 left
💡 Hint
Think about what responseCode and inputStream return when the server responds successfully.
✗ Incorrect
The code sets the request method to GET, connects to the URL, reads the response code and body. If the server returns 200 and "Hello World", the log will show those values.
📝 Syntax
intermediate2:00remaining
Which option correctly sends a POST request with JSON body?
You want to send a POST request with JSON data in Kotlin Android. Which code snippet correctly sets up the connection and sends the JSON string?
Attempts:
2 left
💡 Hint
Remember to enable output and set the content type for JSON when sending POST data.
✗ Incorrect
Option A correctly sets requestMethod to POST, enables output with doOutput=true, sets Content-Type header, and writes JSON bytes to outputStream.
❓ lifecycle
advanced2:00remaining
What is the risk of running network requests on the main thread?
In Android Kotlin, what happens if you run a GET or POST request on the main UI thread?
Attempts:
2 left
💡 Hint
Think about what happens when the UI thread is blocked for too long.
✗ Incorrect
Network requests can take time. Running them on the main thread blocks UI updates, causing the app to freeze and possibly trigger an ANR error.
🔧 Debug
advanced2:00remaining
Why does this POST request code throw a ProtocolException?
This Kotlin code throws java.net.ProtocolException: cannot write output after reading input. Why?
Android Kotlin
val url = URL("https://example.com/api/post") val conn = url.openConnection() as HttpURLConnection conn.requestMethod = "POST" conn.doOutput = true val response = conn.inputStream.bufferedReader().readText() conn.outputStream.write("data".toByteArray())
Attempts:
2 left
💡 Hint
Check the order of reading input and writing output streams.
✗ Incorrect
You must write data to outputStream before reading inputStream. Reading inputStream first locks the connection and causes ProtocolException.
🧠 Conceptual
expert3:00remaining
How to handle JSON response parsing after a GET request?
After a successful GET request returning JSON data, which approach correctly parses the JSON string into a Kotlin data class?
Android Kotlin
data class User(val id: Int, val name: String) val jsonResponse = "{\"id\":1, \"name\":\"Alice\"}"
Attempts:
2 left
💡 Hint
Use a JSON library to convert JSON string to Kotlin objects.
✗ Incorrect
Gson().fromJson() correctly parses JSON string into the User data class. Other options either cause errors or do not parse JSON properly.