StrictMode 檢測到主線程網絡 I/O 直接拋出 NetworkOnMainThreadException 崩潰;new Thread(() -> {
// 1. 子線程執行網絡請求
String result = HttpUtils.get(url);
// 2. 通過 Handler/RunOnUiThread 回主線程更新 UI
runOnUiThread(() -> {
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, parse(result)));
});
}).start();
withContext(Dispatchers.IO) { ... };subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());示例中 SystemClock.sleep 僅為模擬網絡延遲,**實際開發中不應人為睡眠**,應依賴回調/掛起機制等待響應。