# android.os.NetworkOnMainThreadException

- **ID:** `android/networkonmainthreadexception`
- **领域:** android
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

在主 UI 线程上执行网络操作，自 Honeycomb 起 Android 的严格模式禁止此行为。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Android 3.0+ | active | — | — |
| minSdkVersion 11+ | active | — | — |

## 解决方案

1. ```
   使用 Kotlin 协程：在 viewModelScope.launch(Dispatchers.IO) { ... } 中包装网络调用
   ```
2. ```
   使用 RxJava：subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
   ```
3. ```
   使用 Thread：new Thread(() -> { /* 网络调用 */ runOnUiThread(() -> updateUI()); }).start()
   ```

## 无效尝试

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