我尝试从我的Android程序连接到我的C#WebService。
当我连接通过模拟器时,它的工作非常出色。
但是当我通过手机连接时,我收到了错误。
我禁用FireWall,并在浏览器中通过手机看到网络服务。
我得到的错误:
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
答案 0 :(得分: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版本是什么?