NetworkOnMainThreadException
Android
结论摘要
NetworkOnMainThreadException indicates a network failure on Android that interrupts the current user flow.
现象表现
- NetworkOnMainThreadException appears in logcat
- The app crashes or the action fails
常见原因
- Endpoint or DNS is unreachable
- TLS, transport, or request policy rejects the call
如何验证
快速修复
- Add guard checks and a safe fallback path
- Disable or rollback the risky code path behind a flag
长期修复
- Enforce contracts with types, runtime checks, and tests
- Refactor ownership and lifecycle boundaries to remove the failure mode
示例堆栈
NetworkOnMainThreadException at com.example.app.MainActivity.onCreate(MainActivity.kt:42)