android.os.NetworkOnMainThreadException
The app made a network call on the main (UI) thread, which Android forbids.
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
- Move the call to Dispatchers.IO (viewModelScope.launch)
- Use Retrofit suspend functions or enqueue()
- Never use runBlocking on the main thread
Detailed fix by platform
Kotlin
- kotlin
viewModelScope.launch { val user = withContext(Dispatchers.IO) { api.getUser(id) } _state.value = user }
How to diagnose
- Stack — Which call in onCreate/onClick?
- 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:
Similar errors
- actively refused it HttpRequestException: No connection could be made because the target machine actively refused it (localhost:5001)
- ANR ANR: Application Not Responding (Input dispatching timed out)
- CalledFromWrongThreadException CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views
Most viewed in Android
- Could not resolve Gradle: Could not resolve all files for configuration / Could not resolve dependency
- Unsupported class file major version Gradle: Unsupported class file major version / Android Gradle plugin requires Java 17 to run
- INSTALL_PARSE_FAILED_NO_CERTIFICATES INSTALL_PARSE_FAILED_NO_CERTIFICATES / App not installed as package appears to be invalid
Other ways to find it
Where this error sits
Origin: On your device. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 14 Oct 2026