在哪些情况下可以在 UI 线程上进行网络调用?

In which situations is it acceptable to make network calls on the UI Thread?

这个问题在大多数 Android 考试中被问到,但我没有从任何 android 论坛得到任何答案。

提前致谢。

永远永远永远永远永远永远永远永远。

永远。

在 Android 上完全不可能做到这一点。

如果您尝试在 UI 线程上执行此操作,

Android 本身将抛出异常。

https://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

直接来自Android documentation

To avoid creating an unresponsive UI, don't perform network operations on the UI thread. By default, Android 3.0 (API level 11) and higher requires you to perform network operations on a thread other than the main UI thread; if you don't, a NetworkOnMainThreadException is thrown.

在 UI 线程上进行网络调用是不可接受的。