NetworkOnMainThreadException Android Origin: On your device Last updated 14 Oct 2026

android.os.NetworkOnMainThreadException

The app made a network call on the main (UI) thread, which Android forbids.

Seen on: Android

Meaning

Network calls must run in a background thread — Kotlin coroutines with Dispatchers.IO, an Executor, WorkManager or a library like Retrofit with async calls/suspend functions.

Common causes

  • HttpURLConnection/OkHttp execute() called in an Activity/Fragment directly
  • runBlocking on the main thread
  • Synchronous SDK call in onCreate

⚡ Quick fix

  1. Move the call to Dispatchers.IO (viewModelScope.launch)
  2. Use Retrofit suspend functions or enqueue()
  3. Never use runBlocking on the main thread

Detailed fix by platform

Kotlin

  1. kotlin
    viewModelScope.launch {
        val user = withContext(Dispatchers.IO) { api.getUser(id) }
        _state.value = user
    }

How to diagnose

  1. Stack — Which call in onCreate/onClick?
  2. Threading — Dispatcher used

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: On your device. Errors that are often confused with or caused by this one (click to open):

actively refused it — HttpRequestException: No connection could be made because the target machine actively refused it (localhost:5001) (.NET)actively refused itANR — ANR: Application Not Responding (Input dispatching timed out) (Android)ANRCalledFromWrongThreadException — CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views (Android)CalledFromWrongThread…Cannot access database on the main thread — Room: Cannot access database on the main thread since it may potentially lock the UI for a long period of time (Android)Cannot access databas…CannotPullContainerError — ECS task stopped: CannotPullContainerError: pull image manifest has been retried ... / ref pull has been retried (AWS)CannotPullContainerEr…default backend - 404 — Ingress returns "404 Not Found" / "default backend - 404" (ingress-nginx) (Kubernetes)default backend - 404DNS in pod — DNS not working inside a Kubernetes pod (could not resolve host / nslookup: can't resolve) (Kubernetes)DNS in podFailedCreatePodSandBox — Failed to create pod sandbox: plugin type="calico" failed (add): ... (FailedCreatePodSandBox) (Kubernetes)FailedCreatePodSandBoxNetworkOn…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.