# android.os.NetworkOnMainThreadException

- **ID:** `android/networkonmainthreadexception`
- **Domain:** android
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 92%

## Root Cause

Performing a network operation on the main UI thread, which is forbidden by Android's strict mode since Honeycomb.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Android 3.0+ | active | — | — |
| minSdkVersion 11+ | active | — | — |

## Workarounds

1. **Use Kotlin Coroutines: wrap network call in viewModelScope.launch(Dispatchers.IO) { ... }** (95% success)
   ```
   Use Kotlin Coroutines: wrap network call in viewModelScope.launch(Dispatchers.IO) { ... }
   ```
2. **Use RxJava: subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())** (90% success)
   ```
   Use RxJava: subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
   ```
3. **Use Thread: new Thread(() -> { /* network call */ runOnUiThread(() -> updateUI()); }).start()** (80% success)
   ```
   Use Thread: new Thread(() -> { /* network call */ runOnUiThread(() -> updateUI()); }).start()
   ```

## Dead Ends

- **** — This only allows cleartext HTTP, but does not move the network call off the main thread. (70% fail)
- **** — AsyncTask is deprecated and can still leak context or cause threading issues; not a modern solution. (50% fail)
- **** — This only suppresses the exception for debugging, not a production fix; network on main thread still blocks UI. (90% fail)
