This question already has answers here:
NetworkOnMainThreadException

(5个答案)


3年前关闭。




我的目标是将浮点数数组转换为字符串数组,并通过POST请求将其发送到我家中的远程服务器。我没有使用套接字。

URL url = new URL("http:// (destination of the remote server) : (port)");
 URLConnection connection =  url.openConnection();


这就是我编写getInputStream()和getOutputStream方法的方式,每次运行代码时,我的活动都会崩溃。

OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());

reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));


一切正常,但是我的活动进入getOutputStream()并崩溃。我尝试注释掉getOutputStream()部分,然后出现在connection.getInputStream()并崩溃了。

这可能不起作用的原因可能是什么?

堆栈跟踪:

                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hfad.viscosity/com.hfad.viscosity.ProcessData}: android.os.NetworkOnMainThreadException
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                                                                        at android.app.ActivityThread.access$900(ActivityThread.java:157)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5525)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                                                                     Caused by: android.os.NetworkOnMainThreadException
                                                                        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
                                                                        at java.net.InetAddress.lookupHostByName(InetAddress.java:436)
                                                                        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
                                                                        at java.net.InetAddress.getAllByName(InetAddress.java:215)
                                                                        at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
                                                                        at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
                                                                        at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
                                                                        at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
                                                                        at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
                                                                        at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
                                                                        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
                                                                        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
                                                                        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
                                                                        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
                                                                        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245)
                                                                        at com.hfad.viscosity.ProcessData.onCreate(ProcessData.java:110)
                                                                        at android.app.Activity.performCreate(Activity.java:6272)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
                                                                        at android.app.ActivityThread.access$900(ActivityThread.java:157) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:148) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5525) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

最佳答案

看来您正在尝试在活动的主UI线程而不是后台线程上进行网络活动。对于涉及阻止I / O或繁重的处理(包括网络活动)的任何活动都需要在后台线程上进行。因此,您需要使用AsyncTask功能来实现此用例。

请参阅此SO question作为示例

第二件事是要从您的应用程序执行Internet访问,您需要向您的应用程序提供Internet访问权限。请参考该其他SO question,它应有助于您如何提供此权限。

如果即使执行了上述步骤后仍然遇到任何问题,建议您发布整个源代码(如果可能或等效示例),以便更多的人来帮助您。

09-03 17:16