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
- 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
Other ways to find it
🧠 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.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026