0
0
Android Kotlinmobile~5 mins

GET and POST requests in Android Kotlin

Choose your learning style9 modes available
Introduction

GET and POST requests let your app talk to the internet. GET asks for data, POST sends data.

When your app needs to get information from a website or server.
When your app needs to send user input or data to a server.
When you want to load a list of items from the internet.
When you want to submit a form or save data online.
When your app communicates with online services or APIs.
Syntax
Android Kotlin
val url = URL("https://example.com/api")

// GET request
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"

// POST request
val connectionPost = url.openConnection() as HttpURLConnection
connectionPost.requestMethod = "POST"
connectionPost.doOutput = true
val outputStream = connectionPost.outputStream
outputStream.write(postData.toByteArray())
outputStream.flush()

Use GET to request data from a server.

Use POST to send data to a server, like form info.

Examples
This example shows a simple GET request to get data from a server.
Android Kotlin
val url = URL("https://api.example.com/data")
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
val response = connection.inputStream.bufferedReader().readText()
This example sends data using POST to the server, like submitting a form.
Android Kotlin
val url = URL("https://api.example.com/submit")
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "POST"
connection.doOutput = true
val postData = "name=John&age=30"
connection.outputStream.use { it.write(postData.toByteArray()) }
val response = connection.inputStream.bufferedReader().readText()
Sample App

This app makes a GET request to get a post and a POST request to send data. It prints the server responses in the console.

Android Kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.net.HttpURLConnection
import java.net.URL

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(android.R.layout.simple_list_item_1)

        CoroutineScope(Dispatchers.IO).launch {
            val getResponse = makeGetRequest("https://jsonplaceholder.typicode.com/posts/1")
            val postResponse = makePostRequest("https://jsonplaceholder.typicode.com/posts", "title=Hello&body=World")

            println("GET response: $getResponse")
            println("POST response: $postResponse")
        }
    }

    private fun makeGetRequest(urlString: String): String {
        val url = URL(urlString)
        val connection = url.openConnection() as HttpURLConnection
        connection.requestMethod = "GET"
        return connection.inputStream.bufferedReader().readText()
    }

    private fun makePostRequest(urlString: String, postData: String): String {
        val url = URL(urlString)
        val connection = url.openConnection() as HttpURLConnection
        connection.requestMethod = "POST"
        connection.doOutput = true
        connection.outputStream.use { it.write(postData.toByteArray()) }
        return connection.inputStream.bufferedReader().readText()
    }
}
OutputSuccess
Important Notes

Network calls must run off the main thread to avoid freezing the app.

Use libraries like Retrofit or Ktor for easier network requests in real apps.

Always check for errors and handle exceptions when making requests.

Summary

GET requests fetch data from a server.

POST requests send data to a server.

Network calls should run in background threads to keep the app smooth.