不幸的是App已停止 - android.os.NetworkOnMainThreadException

时间:2013-04-11 09:00:51

标签: android

我正在编写一个应用程序,其中我允许用户输入他们的详细信息,但我收到错误,每当用户点击提交按钮时,错误说:不幸的是应用已停止

android.os.NetworkOnMainThreadException

Logcat说:

04-11 14:25:07.297: E/AndroidRuntime(971): FATAL EXCEPTION: main
04-11 14:25:07.297: E/AndroidRuntime(971): android.os.NetworkOnMainThreadException
04-11 14:25:07.297: E/AndroidRuntime(971):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-11 14:25:07.297: E/AndroidRuntime(971):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
04-11 14:25:07.297: E/AndroidRuntime(971):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
04-11 14:25:07.297: E/AndroidRuntime(971):  at libcore.io.IoBridge.connect(IoBridge.java:112)
04-11 14:25:07.297: E/AndroidRuntime(971):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-11 14:25:07.297: E/AndroidRuntime(971):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
04-11 14:25:07.297: E/AndroidRuntime(971):  at java.net.Socket.connect(Socket.java:842)
04-11 14:25:07.297: E/AndroidRuntime(971):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
04-11 14:25:07.297: E/AndroidRuntime(971):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
04-11 14:25:07.297: E/AndroidRuntime(971):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-11 14:25:07.297: E/AndroidRuntime(971):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-11 14:25:07.297: E/AndroidRuntime(971):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-11 14:25:07.297: E/AndroidRuntime(971):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-11 14:25:07.297: E/AndroidRuntime(971):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-11 14:25:07.297: E/AndroidRuntime(971):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-11 14:25:07.297: E/AndroidRuntime(971):  at com.erachnida.restaurant.versionoct.cart.RegistrationActivity.getHttpPost(RegistrationActivity.java:183)
04-11 14:25:07.297: E/AndroidRuntime(971):  at com.erachnida.restaurant.versionoct.cart.RegistrationActivity.SaveData(RegistrationActivity.java:135)
04-11 14:25:07.297: E/AndroidRuntime(971):  at com.erachnida.restaurant.versionoct.cart.RegistrationActivity$1.onClick(RegistrationActivity.java:49)
04-11 14:25:07.297: E/AndroidRuntime(971):  at android.view.View.performClick(View.java:4202)
04-11 14:25:07.297: E/AndroidRuntime(971):  at android.view.View$PerformClick.run(View.java:17340)
04-11 14:25:07.297: E/AndroidRuntime(971):  at android.os.Handler.handleCallback(Handler.java:725)
04-11 14:25:07.297: E/AndroidRuntime(971):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 14:25:07.297: E/AndroidRuntime(971):  at android.os.Looper.loop(Looper.java:137)
04-11 14:25:07.297: E/AndroidRuntime(971):  at android.app.ActivityThread.main(ActivityThread.java:5039)
04-11 14:25:07.297: E/AndroidRuntime(971):  at java.lang.reflect.Method.invokeNative(Native Method)
04-11 14:25:07.297: E/AndroidRuntime(971):  at java.lang.reflect.Method.invoke(Method.java:511)
04-11 14:25:07.297: E/AndroidRuntime(971):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-11 14:25:07.297: E/AndroidRuntime(971):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-11 14:25:07.297: E/AndroidRuntime(971):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:4)

您无法在更高版本的Android 中调用主线程中的网络操作。因此,请执行一个工作线程或异步任务来执行网络操作,如调用 Webservice ,下载图像等。

这不允许使Android应用程序更具响应性和流畅性

答案 1 :(得分:0)

当您在蜂窝和更高版本的主线程上建立网络连接时,会发生这种情况。您应该使用AsyncTask执行所有网络操作。 AsyncTask在后​​台线程上执行操作。点击herehere以了解AsyncTask