0

我尝试从我的 android 程序连接到我的 C# WebService。

当我通过模拟器连接时,它的工作非常出色。

但是当我通过电话连接时出现错误。

我禁用了防火墙,并从手机的浏览器中看到了网络服务。

我得到的错误:

10-23 20:02:14.697: W/System.err(25237): android.os.NetworkOnMainThreadException
10-23 20:02:14.697: W/System.err(25237):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-23 20:02:14.697: W/System.err(25237):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
10-23 20:02:14.697: W/System.err(25237):    at libcore.io.IoBridge.connectErrno(IoBridge.java:138)
10-23 20:02:14.697: W/System.err(25237):    at libcore.io.IoBridge.connect(IoBridge.java:112)
10-23 20:02:14.697: W/System.err(25237):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-23 20:02:14.702: W/System.err(25237):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-23 20:02:14.702: W/System.err(25237):    at java.net.Socket.connect(Socket.java:842)
10-23 20:02:14.702: W/System.err(25237):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
10-23 20:02:14.702: W/System.err(25237):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-23 20:02:14.702: W/System.err(25237):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-23 20:02:14.702: W/System.err(25237):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-23 20:02:14.702: W/System.err(25237):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-23 20:02:14.702: W/System.err(25237):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-23 20:02:14.707: W/System.err(25237):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
10-23 20:02:14.707: W/System.err(25237):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-23 20:02:14.707: W/System.err(25237):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
10-23 20:02:14.707: W/System.err(25237):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-23 20:02:14.707: W/System.err(25237):    at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:76)
10-23 20:02:14.707: W/System.err(25237):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:153)
10-23 20:02:14.707: W/System.err(25237):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
10-23 20:02:14.707: W/System.err(25237):    at com.example.eitanws_exmp.WS.HelloWorld(WS.java:198)
10-23 20:02:14.707: W/System.err(25237):    at com.example.eitanws_exmp.MainActivity.GetArray_Click(MainActivity.java:53)
10-23 20:02:14.712: W/System.err(25237):    at java.lang.reflect.Method.invokeNative(Native Method)
10-23 20:02:14.712: W/System.err(25237):    at java.lang.reflect.Method.invoke(Method.java:511)
10-23 20:02:14.712: W/System.err(25237):    at android.view.View$1.onClick(View.java:3064)
10-23 20:02:14.712: W/System.err(25237):    at android.view.View.performClick(View.java:3591)
10-23 20:02:14.712: W/System.err(25237):    at android.view.View$PerformClick.run(View.java:14263)
10-23 20:02:14.712: W/System.err(25237):    at android.os.Handler.handleCallback(Handler.java:605)
10-23 20:02:14.712: W/System.err(25237):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 20:02:14.712: W/System.err(25237):    at android.os.Looper.loop(Looper.java:137)
10-23 20:02:14.717: W/System.err(25237):    at android.app.ActivityThread.main(ActivityThread.java:4507)
10-23 20:02:14.717: W/System.err(25237):    at java.lang.reflect.Method.invokeNative(Native Method)
10-23 20:02:14.717: W/System.err(25237):    at java.lang.reflect.Method.invoke(Method.java:511)
10-23 20:02:14.717: W/System.err(25237):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
10-23 20:02:14.717: W/System.err(25237):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-23 20:02:14.717: W/System.err(25237):    at dalvik.system.NativeStart.main(Native Method)
10-23 20:02:14.722: W/System.err(25237): android.os.NetworkOnMainThreadException
10-23 20:02:14.727: W/System.err(25237):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-23 20:02:14.727: W/System.err(25237):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
10-23 20:02:14.727: W/System.err(25237):    at libcore.io.IoBridge.connectErrno(IoBridge.java:138)
10-23 20:02:14.727: W/System.err(25237):    at libcore.io.IoBridge.connect(IoBridge.java:112)
10-23 20:02:14.727: W/System.err(25237):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-23 20:02:14.727: W/System.err(25237):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-23 20:02:14.732: W/System.err(25237):    at java.net.Socket.connect(Socket.java:842)
10-23 20:02:14.732: W/System.err(25237):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
10-23 20:02:14.732: W/System.err(25237):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-23 20:02:14.732: W/System.err(25237):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-23 20:02:14.732: W/System.err(25237):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-23 20:02:14.732: W/System.err(25237):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-23 20:02:14.737: W/System.err(25237):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-23 20:02:14.737: W/System.err(25237):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
10-23 20:02:14.737: W/System.err(25237):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-23 20:02:14.737: W/System.err(25237):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
10-23 20:02:14.737: W/System.err(25237):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-23 20:02:14.737: W/System.err(25237):    at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:76)
10-23 20:02:14.737: W/System.err(25237):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:153)
10-23 20:02:14.737: W/System.err(25237):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
10-23 20:02:14.737: W/System.err(25237):    at com.example.eitanws_exmp.WS.WebServiceCallExampleGetArray(WS.java:88)
10-23 20:02:14.737: W/System.err(25237):    at com.example.eitanws_exmp.MainActivity.GetArray_Click(MainActivity.java:54)
10-23 20:02:14.742: W/System.err(25237):    at java.lang.reflect.Method.invokeNative(Native Method)
10-23 20:02:14.747: W/System.err(25237):    at java.lang.reflect.Method.invoke(Method.java:511)
10-23 20:02:14.752: W/System.err(25237):    at android.view.View$1.onClick(View.java:3064)
10-23 20:02:14.752: W/System.err(25237):    at android.view.View.performClick(View.java:3591)
10-23 20:02:14.752: W/System.err(25237):    at android.view.View$PerformClick.run(View.java:14263)
10-23 20:02:14.752: W/System.err(25237):    at android.os.Handler.handleCallback(Handler.java:605)
10-23 20:02:14.752: W/System.err(25237):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 20:02:14.752: W/System.err(25237):    at android.os.Looper.loop(Looper.java:137)
10-23 20:02:14.752: W/System.err(25237):    at android.app.ActivityThread.main(ActivityThread.java:4507)
10-23 20:02:14.752: W/System.err(25237):    at java.lang.reflect.Method.invokeNative(Native Method)
10-23 20:02:14.752: W/System.err(25237):    at java.lang.reflect.Method.invoke(Method.java:511)
10-23 20:02:14.752: W/System.err(25237):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
10-23 20:02:14.752: W/System.err(25237):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-23 20:02:14.757: W/System.err(25237):    at dalvik.system.NativeStart.main(Native Method)
10-23 20:02:14.757: W/System.err(25237): android.os.NetworkOnMainThreadException
10-23 20:02:14.762: W/System.err(25237):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-23 20:02:14.762: W/System.err(25237):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
10-23 20:02:14.762: W/System.err(25237):    at libcore.io.IoBridge.connectErrno(IoBridge.java:138)
10-23 20:02:14.762: W/System.err(25237):    at libcore.io.IoBridge.connect(IoBridge.java:112)
10-23 20:02:14.762: W/System.err(25237):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-23 20:02:14.762: W/System.err(25237):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-23 20:02:14.762: W/System.err(25237):    at java.net.Socket.connect(Socket.java:842)
10-23 20:02:14.762: W/System.err(25237):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
10-23 20:02:14.762: W/System.err(25237):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-23 20:02:14.762: W/System.err(25237):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-23 20:02:14.762: W/System.err(25237):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-23 20:02:14.762: W/System.err(25237):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-23 20:02:14.762: W/System.err(25237):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-23 20:02:14.767: W/System.err(25237):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
10-23 20:02:14.767: W/System.err(25237):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-23 20:02:14.767: W/System.err(25237):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
10-23 20:02:14.767: W/System.err(25237):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-23 20:02:14.767: W/System.err(25237):    at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:76)
10-23 20:02:14.767: W/System.err(25237):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:153)
10-23 20:02:14.767: W/System.err(25237):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
10-23 20:02:14.767: W/System.err(25237):    at com.example.eitanws_exmp.WS.WebServiceCallExampleGetArray(WS.java:88)
10-23 20:02:14.767: W/System.err(25237):    at com.example.eitanws_exmp.MainActivity.GetArray_Click(MainActivity.java:56)
10-23 20:02:14.767: W/System.err(25237):    at java.lang.reflect.Method.invokeNative(Native Method)
10-23 20:02:14.767: W/System.err(25237):    at java.lang.reflect.Method.invoke(Method.java:511)
10-23 20:02:14.767: W/System.err(25237):    at android.view.View$1.onClick(View.java:3064)
10-23 20:02:14.767: W/System.err(25237):    at android.view.View.performClick(View.java:3591)
10-23 20:02:14.767: W/System.err(25237):    at android.view.View$PerformClick.run(View.java:14263)
10-23 20:02:14.772: W/System.err(25237):    at android.os.Handler.handleCallback(Handler.java:605)
10-23 20:02:14.772: W/System.err(25237):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 20:02:14.772: W/System.err(25237):    at android.os.Looper.loop(Looper.java:137)
10-23 20:02:14.772: W/System.err(25237):    at android.app.ActivityThread.main(ActivityThread.java:4507)
10-23 20:02:14.772: W/System.err(25237):    at java.lang.reflect.Method.invokeNative(Native Method)
10-23 20:02:14.772: W/System.err(25237):    at java.lang.reflect.Method.invoke(Method.java:511)
10-23 20:02:14.772: W/System.err(25237):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
10-23 20:02:14.772: W/System.err(25237):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-23 20:02:14.772: W/System.err(25237):    at dalvik.system.NativeStart.main(Native Method)
10-23 20:02:14.772: D/AndroidRuntime(25237): Shutting down VM
10-23 20:02:14.772: W/dalvikvm(25237): threadid=1: thread exiting with uncaught exception (group=0x40c511f8)
10-23 20:02:14.782: E/AndroidRuntime(25237): FATAL EXCEPTION: main
10-23 20:02:14.782: E/AndroidRuntime(25237): java.lang.IllegalStateException: Could not execute method of the activity
10-23 20:02:14.782: E/AndroidRuntime(25237):    at android.view.View$1.onClick(View.java:3069)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at android.view.View.performClick(View.java:3591)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at android.view.View$PerformClick.run(View.java:14263)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at android.os.Handler.handleCallback(Handler.java:605)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at android.os.Looper.loop(Looper.java:137)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at android.app.ActivityThread.main(ActivityThread.java:4507)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at java.lang.reflect.Method.invokeNative(Native Method)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at java.lang.reflect.Method.invoke(Method.java:511)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at dalvik.system.NativeStart.main(Native Method)
10-23 20:02:14.782: E/AndroidRuntime(25237): Caused by: java.lang.reflect.InvocationTargetException
10-23 20:02:14.782: E/AndroidRuntime(25237):    at java.lang.reflect.Method.invokeNative(Native Method)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at java.lang.reflect.Method.invoke(Method.java:511)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at android.view.View$1.onClick(View.java:3064)
10-23 20:02:14.782: E/AndroidRuntime(25237):    ... 11 more
10-23 20:02:14.782: E/AndroidRuntime(25237): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 4, size is 0
10-23 20:02:14.782: E/AndroidRuntime(25237):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at java.util.ArrayList.get(ArrayList.java:304)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at com.example.eitanws_exmp.MainActivity.GetArray_Click(MainActivity.java:56)
10-23 20:02:14.782: E/AndroidRuntime(25237):    ... 14 more
4

1 回答 1

0
Caused by: java.lang.IndexOutOfBoundsException: **Invalid index 4, size is 0**
10-23 20:02:14.782: E/AndroidRuntime(25237):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at java.util.ArrayList.get(ArrayList.java:304)
10-23 20:02:14.782: E/AndroidRuntime(25237):    at com.example.eitanws_exmp.MainActivity.GetArray_Click(**MainActivity.java:56)**

尝试使用数组时,该应用程序似乎超出了范围。看:无效的索引4,大小为0 你在应用程序中使用任何数组吗?尝试调试这一行及其周围:MainActivity.java:56 - 似乎应用程序试图访问一个空数组。

更新

关于 NetworkOnMainThreadException

“当应用程序尝试在其主线程上执行网络操作时引发的异常。”

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

你的 SDK 版本是多少?

于 2012-10-23T19:24:42.733 回答