我正在尝试从 SOAP 连接到 Web 服务。我在 PHP 中开发了 SOAP Webservice,它运行良好(在 Visual Studio 中尝试过)。
首先,这是错误:
09-27 06:28:07.724: E/AndroidRuntime(2057): Caused by: android.os.NetworkOnMainThreadException
09-27 06:28:07.724: E/AndroidRuntime(2057): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.io.IoBridge.connect(IoBridge.java:112)
09-27 06:28:07.724: E/AndroidRuntime(2057): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-27 06:28:07.724: E/AndroidRuntime(2057): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
09-27 06:28:07.724: E/AndroidRuntime(2057): at java.net.Socket.connect(Socket.java:842)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
09-27 06:28:07.724: E/AndroidRuntime(2057): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
09-27 06:28:07.724: E/AndroidRuntime(2057): at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:126)
09-27 06:28:07.724: E/AndroidRuntime(2057): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:179)
09-27 06:28:07.724: E/AndroidRuntime(2057): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
09-27 06:28:07.724: E/AndroidRuntime(2057): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111)
09-27 06:28:07.724: E/AndroidRuntime(2057): at com.example.FirstExample.CategoriesActivity.onCreate(CategoriesActivity.java:49)
09-27 06:28:07.724: E/AndroidRuntime(2057): at android.app.Activity.performCreate(Activity.java:5133)
09-27 06:28:07.724: E/AndroidRuntime(2057): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-27 06:28:07.724: E/AndroidRuntime(2057): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-27 06:28:07.724: E/AndroidRuntime(2057): ... 11 more
您可以在此处查看完整错误:http: //pastebin.com/cRQ66vrj
这是我的 Java 代码:
private final String NAMESPACE = "http://10.0.0.20/soap/test_wsdl";
private final String URL = "http://10.0.0.20/info_send/encode.php?wsdl";
private final String SOAP_ACTION = "http://10.0.0.20/info_send/encode.php/select_data";
private final String METHOD_NAME = "select_data";
private String Webresponse = "";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response;
response = (SoapPrimitive)envelope.getResponse();
Webresponse = response.toString();
} catch (SoapFault e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
Log.e("VALUE: ", Webresponse);
我一直在关注几个教程,但没有一个适合我的需求。所以我正在尝试创建一种方法来满足我的需求。另外,我添加到清单:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
我究竟做错了什么?另外,如果你能给我一个方向,以便我可以逐项获取使用上面的代码检索到的值,我会感谢你的。就像是:
for(int i = 0; i <= variable.size; i++){
Log.e("Value: ", variable[i].ToString())
}
谢谢。