Ksoap2 无法在 android 3.0 中工作并出现 NetworkOnMainThreadException
Ksoap2 无法在 Android 3.0 中运行。 Logcat 显示 NetworkOnMainThreadException:
02-24 20:18:34.536: ERROR/AndroidRuntime(428): java.lang.RuntimeException: Unable to start activity ComponentInfo{com. Bill/com. Bill.Diag}: android.os.NetworkOnMainThreadException
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1701)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1717)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:984)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.os.Looper.loop(Looper.java:126)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.ActivityThread.main(ActivityThread.java:3900)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at java.lang.reflect.Method.invoke(Method.java:491)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at dalvik.system.NativeStart.main(Native Method)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): Caused by: android.os.NetworkOnMainThreadException
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1069)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:368)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:208)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at java.net.Socket.connect(Socket.java:901)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:217)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at com.IsBill.Connection.Connect(Connection.java:53)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at com.IsBill.Diagnos.FirstProv(Diagnos.java:385)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at com.IsBill.Diagnos.onCreate(Diagnos.java:53)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1665)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): ... 11 more
Ksoap2 is not working in Android 3.0. Logcat shows NetworkOnMainThreadException:
02-24 20:18:34.536: ERROR/AndroidRuntime(428): java.lang.RuntimeException: Unable to start activity ComponentInfo{com. Bill/com. Bill.Diag}: android.os.NetworkOnMainThreadException
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1701)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1717)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:984)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.os.Looper.loop(Looper.java:126)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.ActivityThread.main(ActivityThread.java:3900)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at java.lang.reflect.Method.invoke(Method.java:491)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at dalvik.system.NativeStart.main(Native Method)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): Caused by: android.os.NetworkOnMainThreadException
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1069)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:368)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:208)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at java.net.Socket.connect(Socket.java:901)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:217)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at com.IsBill.Connection.Connect(Connection.java:53)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at com.IsBill.Diagnos.FirstProv(Diagnos.java:385)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at com.IsBill.Diagnos.onCreate(Diagnos.java:53)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1665)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): ... 11 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HoneyComb 中启用了 StrictMode,禁用它以避免 NetworkOnMainThreadException
更新:
其他检测方式是使用 StrictModeWrapper 类存在 strictMode。
StrictModeWrapper 类
StrictMode is enabled in HoneyComb, disable it to avoid NetworkOnMainThreadException
UPDATE:
other way detecting is strictMode exist using StrictModeWrapper class.
StrictModeWrapper Class
您需要在单独的线程中而不是在 UI 线程中执行任何网络操作。最简单的方法是使用 AsyncTask。
You need to do any network operation in a separate thread and not on the UI thread. The easiest way to do that is using AsyncTask.
网络操作必须在新线程上进行处理
您应该在 AsyncTask 任务类中调用 ksoap 请求
在您的 Asynctask 中调用您的 KSOAP 请求
doInBackground()
方法例如
}
Network operation must handle on new thread to do so
You should call ksoap request in an AsyncTask task class
in your Asynctask Call your KSOAP reuest in
doInBackground()
methode.g.
}