1

我在 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)

这让我疯狂。

4

1 回答 1

1

您的代码没有任何问题。req前缀符合 SOAP/XML 标准。问题在于 Web 服务的部署方式和ksoap工作方式。

?wsdl如果您附加到 Web 服务端点,则所有 Web 服务都应使用 WSDL 进行响应。ksoap期望这一点并请求 WSDL。在这种情况下,Web 服务不会发回 WSDL,从而引发 400 错误。

您应该询问 Web 服务提供者为什么在您发送http://whatever.com/WebService/Request.asmx?wsdl.

如果这不起作用,请考虑在服务器上创建一个 SOAP 客户端,连接到 Web 服务,然后使用 Android 的 REST Web 服务调用您的服务器。这样,您可以使用更少的带宽并在设备上进行更少的处理。

于 2013-04-22T05:23:02.710 回答