bobowg / BaiseComposeLearn

android compose 基本参考用法与示例
4 stars 0 forks source link

android.os.NetworkOnMainThreadException 网络异常 #11

Open bobowg opened 2 years ago

bobowg commented 2 years ago

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged. See the document Designing for Responsiveness.

Also see StrictMode. 所以事情就很清楚了。一个APP如果在主线程中请求网络操作,将会抛出此异常。Android这个设计是为了防止网络请求时间过长而导致界面假死的情况发生。

bobowg commented 2 years ago

if (Build.VERSION.SDK_INT > 9) { val policy = ThreadPolicy.Builder().permitAll().build() StrictMode.setThreadPolicy(policy) }