Bird
Raised Fist0
Android Kotlinmobile~20 mins

GET and POST requests in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Network Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2: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")
ACode: 200, Body: null
BCode: 404, Body: Not Found
CCode: 200, Body: Hello World
DApp crashes with IOException
Attempts:
2 left
💡 Hint
Think about what responseCode and inputStream return when the server responds successfully.
📝 Syntax
intermediate
2: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?
A
val url = URL("https://example.com/api/data")
val conn = url.openConnection() as HttpURLConnection
conn.requestMethod = "POST"
conn.doOutput = true
conn.setRequestProperty("Content-Type", "application/json")
val json = "{\"name\":\"John\"}"
conn.outputStream.write(json.toByteArray())
B
val url = URL("https://example.com/api/data")
val conn = url.openConnection() as HttpURLConnection
conn.requestMethod = "POST"
val json = "{\"name\":\"John\"}"
conn.outputStream.write(json.toByteArray())
C
val url = URL("https://example.com/api/data")
val conn = url.openConnection() as HttpURLConnection
conn.requestMethod = "GET"
conn.doOutput = true
val json = "{\"name\":\"John\"}"
conn.outputStream.write(json.toByteArray())
D
val url = URL("https://example.com/api/data")
val conn = url.openConnection() as HttpURLConnection
conn.requestMethod = "POST"
conn.doInput = true
conn.setRequestProperty("Content-Type", "application/json")
val json = "{\"name\":\"John\"}"
conn.outputStream.write(json.toByteArray())
Attempts:
2 left
💡 Hint
Remember to enable output and set the content type for JSON when sending POST data.
❓ lifecycle
advanced
2: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?
AThe app crashes immediately with a NullPointerException
BThe app UI freezes and may show an Application Not Responding (ANR) error
CThe request runs faster because it uses the main thread
DNothing special happens; network requests are safe on the main thread
Attempts:
2 left
💡 Hint
Think about what happens when the UI thread is blocked for too long.
🔧 Debug
advanced
2: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())
ABecause requestMethod POST is invalid for this URL
BBecause doOutput must be false for POST requests
CBecause outputStream.write() requires a flush() call first
DBecause inputStream was read before enabling output and writing data
Attempts:
2 left
💡 Hint
Check the order of reading input and writing output streams.
🧠 Conceptual
expert
3: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\"}"
Aval user = Gson().fromJson(jsonResponse, User::class.java)
Bval user = jsonResponse.toString() as User
Cval user = User(jsonResponse["id"], jsonResponse["name"])
Dval user = JSONObject(jsonResponse).toString()
Attempts:
2 left
💡 Hint
Use a JSON library to convert JSON string to Kotlin objects.

Practice

(1/5)
1. What is the main purpose of a GET request in Android Kotlin networking?
easy
A. To fetch data from a server
B. To send data to a server
C. To update the UI thread
D. To store data locally

Solution

  1. Step 1: Understand GET request purpose

    A GET request is used to retrieve or fetch data from a server without changing any data on the server.
  2. Step 2: Compare with other request types

    POST requests send data to the server, unlike GET which only fetches data.
  3. Final Answer:

    To fetch data from a server -> Option A
  4. Quick Check:

    GET = fetch data [OK]
Hint: GET fetches data; POST sends data [OK]
Common Mistakes:
  • Confusing GET with POST request purpose
  • Thinking GET modifies server data
  • Mixing UI updates with network requests
2. Which of the following is the correct way to start a POST request using Kotlin's HttpURLConnection?
easy
A. connection.requestMethod = "GET"
B. connection.requestMethod = "POST"
C. connection.method = "POST"
D. connection.setRequestMethod("GET")

Solution

  1. Step 1: Identify correct property for request method

    In Kotlin, HttpURLConnection uses requestMethod property to set the HTTP method.
  2. Step 2: Confirm correct method value for POST

    To make a POST request, set requestMethod to "POST" exactly.
  3. Final Answer:

    connection.requestMethod = "POST" -> Option B
  4. Quick Check:

    POST method set by requestMethod = "POST" [OK]
Hint: Use requestMethod property with "POST" string [OK]
Common Mistakes:
  • Using wrong property name like method
  • Setting GET instead of POST
  • Using Java-style setRequestMethod() in Kotlin
3. What will be the output of this Kotlin code snippet when making a GET request?
val url = URL("https://example.com/data")
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
val responseCode = connection.responseCode
println(responseCode)
medium
A. 500
B. 404
C. 200
D. Compilation error

Solution

  1. Step 1: Understand the code flow

    The code opens a connection to the URL and sets the method to GET, then reads the response code from the server.
  2. Step 2: Predict typical HTTP response code

    Assuming the URL is valid and reachable, a successful GET request returns HTTP status code 200.
  3. Final Answer:

    200 -> Option C
  4. Quick Check:

    Successful GET returns 200 [OK]
Hint: 200 means OK response from server [OK]
Common Mistakes:
  • Assuming responseCode is a string
  • Confusing 404 (not found) with success
  • Thinking code will not compile
4. Identify the error in this Kotlin code snippet for sending a POST request:
val url = URL("https://example.com/api")
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "POST"
val outputStream = connection.outputStream
outputStream.write("data=hello".toByteArray())
outputStream.flush()
outputStream.close()
medium
A. Missing setting connection.doOutput = true
B. Using write() instead of writeBytes()
C. Incorrect requestMethod value
D. Not reading the response code

Solution

  1. Step 1: Check POST request setup

    For POST requests, doOutput must be set to true to send data in the request body.
  2. Step 2: Identify missing property

    The code does not set connection.doOutput = true, so writing to outputStream will fail or not send data.
  3. Final Answer:

    Missing setting connection.doOutput = true -> Option A
  4. Quick Check:

    doOutput = true needed for POST body [OK]
Hint: Set doOutput true before writing POST data [OK]
Common Mistakes:
  • Forgetting doOutput = true for POST
  • Confusing write() method usage
  • Ignoring response reading after POST
5. You want to send JSON data in a POST request using Kotlin. Which sequence correctly sets up the connection and sends the JSON string {"name":"John"}?
hard
A. Set requestMethod to "GET", write JSON bytes to outputStream, set doOutput = true
B. Set requestMethod to "POST", set doOutput = false, write JSON string directly to connection
C. Set requestMethod to "POST", write JSON bytes to outputStream without setting doOutput, set Content-Type header to "text/plain"
D. Set requestMethod to "POST", set doOutput = true, set Content-Type header to "application/json", write JSON bytes to outputStream

Solution

  1. Step 1: Correct HTTP method and output setup

    For sending data, requestMethod must be "POST" and doOutput must be true to allow writing data.
  2. Step 2: Set proper headers and write data

    Content-Type header should be "application/json" to indicate JSON data. Then write JSON bytes to outputStream.
  3. Final Answer:

    Set requestMethod to "POST", set doOutput = true, set Content-Type header to "application/json", write JSON bytes to outputStream -> Option D
  4. Quick Check:

    POST + doOutput + JSON header + write bytes [OK]
Hint: POST JSON needs doOutput true and JSON content-type [OK]
Common Mistakes:
  • Using GET method to send data
  • Not setting Content-Type header correctly
  • Forgetting doOutput = true
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard