android.os.NetworkOnMainThreadException 浅浅的花香味﹌ 2024-04-08 13:01 43阅读 0赞 ### 问题 ### android开发中 出现android.os.NetworkOnMainThreadException异常 具体的 2022-08-27 22:57:17.271 16064-16064/com.example.electronicmall E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.electronicmall, PID: 16064 android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115) at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103) at java.net.InetAddress.getAllByName(InetAddress.java:1152) at com.squareup.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29) at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187) at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156) at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98) at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:344) at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:327) at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245) at com.squareup.okhttp.Call.getResponse(Call.java:267) at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224) at com.example.electronicmall.common.TCLog.intercept(TCLog.java:56) at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:221) at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195) at com.squareup.okhttp.Call.execute(Call.java:79) at com.example.electronicmall.common.http.HttpConnection.doRequest(HttpConnection.java:60) at com.example.electronicmall.common.http.HttpConnection.postRequest(HttpConnection.java:98) at com.example.electronicmall.common.AbstractClient.doRequest(AbstractClient.java:412) at com.example.electronicmall.common.AbstractClient.internalRequest(AbstractClient.java:356) at com.example.electronicmall.sms.v20210111.SmsClient.SendSms(SmsClient.java:403) at com.example.electronicmall.Util.SendSms.main(SendSms.java:118) at com.example.electronicmall.test.User.UserCompleteValidationActivity$2.onClick(UserCompleteValidationActivity.java:52) at android.view.View.performClick(View.java:7317) at android.view.View.performClickInternal(View.java:7291) at android.view.View.access$3600(View.java:838) at android.view.View$PerformClick.run(View.java:28247) at android.os.Handler.handleCallback(Handler.java:900) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8668) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109) 2022-08-27 22:57:17.287 16064-16064/com.example.electronicmall I/Process: Sending signal. PID: 16064 SIG: ### 解决 ### 1 定位出错代码块 ![请添加图片描述][df0bc83029d246fcad8ffdbbc05a4591.png]2 try catch 捕捉异常 ![请添加图片描述][0d09834176d547b781a53f5a92b92679.png] 3 在AndroidManifest.xml文件中添加 <uses-permission android:name="android.permission.INTERNET"/> ### 原因 ### 当应用程序尝试在主线程上执行网络操作,会引发此异常。因此需要捕捉该异常。 参考文献 [How can I fix ‘android.os.NetworkOnMainThreadException’? ][How can I fix _android.os.NetworkOnMainThreadException_] 原创不易 转载请标明出处 如果对你有所帮助 别忘啦点赞支持哈 ![在这里插入图片描述][9cb4cb80a18345158f8a69e2f196e5b9.png] [df0bc83029d246fcad8ffdbbc05a4591.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/08/31f41b9f00df4c0ead52ac05b5be8996.png [0d09834176d547b781a53f5a92b92679.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/08/fb431356307e49808e4e1d041fd56719.png [How can I fix _android.os.NetworkOnMainThreadException_]: https://stackoverflow.com/questions/6343166/how-can-i-fix-android-os-networkonmainthreadexception [9cb4cb80a18345158f8a69e2f196e5b9.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/08/28bf5386a6b54f4c99b5a6673563a556.png
还没有评论,来说两句吧...