问题
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 定位出错代码块
2 try catch 捕捉异常
3 在AndroidManifest.xml文件中添加
<uses-permission android:name="android.permission.INTERNET"/>
原因
当应用程序尝试在主线程上执行网络操作,会引发此异常。因此需要捕捉该异常。
参考文献
How can I fix ‘android.os.NetworkOnMainThreadException’?
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈