我在 android 上使用 ksoap2 库连接到 Web 服务。我运行以下代码:
SoapObject request = new SoapObject(NAMESPACE, method);
request.addProperty("UserName", username);
request.addProperty("TransactionID", id);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
nvelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(NAMESPACE + method, envelope);
如上所示,有两个参数。这是这部分的 wsdl 文件:
<s:element minOccurs="1" maxOccurs="1" name="TransactionID" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="UserName" type="s:string" />
问题是我从 SoapUI 使用相同的参数调用它时得到不同的响应。下面是 SoapUI 请求的样子:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://whatever.com/Webservice/Request.asmx">
<soapenv:Header/>
<soapenv:Body>
<req:GetProduct>
<req:TransactionID>id</req:TransactionID>
<!--Optional:-->
<req:UserName>username</req:UserName>
</req:GetProduct>
参数名称有一个额外的 :req 前缀,可能会导致我的问题。当我将 req: 添加到我的 java 参数名称时,会发生异常:
java.io.IOException: Server returned HTTP response code: 400 for URL: http://whatever.com/WebService/Request.asmx?wsdl/
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1345)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1339)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:993)
at org.ksoap2.transport.ServiceConnectionSE.openInputStream(ServiceConnectionSE.java:113)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:184)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:96)
at SoapCommand.invokeMethod(SoapCommand.java:42)
at SoapCommand.main(SoapCommand.java:16)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: http://whatever.com/WebService/Request.asmx?wsdl/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1290)
at sun.net.www.protocol.http.HttpURLConnection.getHeaderFields(HttpURLConnection.java:2129)
at org.ksoap2.transport.ServiceConnectionSE.getResponseProperties(ServiceConnectionSE.java:84)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:167)
... 3 more
Exception in thread "main" java.lang.NullPointerException
at SoapCommand.main(SoapCommand.java:17)
这让我疯狂。