-3

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

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)
4

2 回答 2

4

在更高版本的 Android 中,您不能在主线程中调用网络操作。所以采取一个工作线程或异步任务来执行网络操作,如调用Web服务,下载图像等。

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

于 2013-04-11T09:02:11.517 回答
0

当您在蜂窝和更高版本的主线程上建立网络连接时,就会发生这种情况。您应该使用它AsyncTask来执行所有网络操作。AsyncTask在后台线程上执行您的操作。单击此处此处了解有关AsyncTask.

于 2013-04-11T09:05:05.637 回答