0
0
Android Kotlinmobile~5 mins

Why network calls fetch remote data in Android Kotlin

Choose your learning style9 modes available
Introduction

Network calls let your app get fresh information from the internet. This helps your app show up-to-date data that is not stored on the device.

When your app needs to show the latest news or weather updates.
When users log in and you need to check their credentials on a server.
When your app displays social media posts or messages from friends.
When you want to download images or videos from a website.
When syncing user data between devices or cloud storage.
Syntax
Android Kotlin
val url = URL("https://example.com/data.json")
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
val inputStream = connection.inputStream
val data = inputStream.bufferedReader().readText()
Use HttpURLConnection to open a connection to a web address.
Always perform network calls on a background thread to avoid freezing the app UI.
Examples
This example fetches user data from a web API using a GET request.
Android Kotlin
val url = URL("https://api.example.com/users")
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
val response = connection.inputStream.bufferedReader().readText()
This example downloads an image file from the internet.
Android Kotlin
val url = URL("https://example.com/image.png")
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
val imageStream = connection.inputStream
Sample App

This app fetches a small JSON object from the internet and shows it on the screen. It uses a background thread to avoid freezing the app.

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
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val textView = TextView(this)
        setContentView(textView)

        CoroutineScope(Dispatchers.IO).launch {
            val url = URL("https://jsonplaceholder.typicode.com/todos/1")
            val connection = url.openConnection() as HttpURLConnection
            connection.requestMethod = "GET"
            val data = connection.inputStream.bufferedReader().readText()
            runOnUiThread {
                textView.text = data
            }
        }
    }
}
OutputSuccess
Important Notes

Never do network calls on the main thread; it will freeze the app.

Always check for internet connection before making network calls.

Handle errors like timeouts or no response gracefully to improve user experience.

Summary

Network calls let apps get fresh data from the internet.

They are used when data changes often or is too big to store locally.

Always run network calls on background threads to keep the app smooth.