2013-04-21 129 views
1

我使用kso​​ap2库在Android上连接到Web服务错误的反应。我运行以下代码:充分利用ksoap2在Android

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) 

这让我疯狂。

回答

1

没有什么不对您的代码。 req前缀按照SOAP/XML标准。问题在于webservice的部署方式以及ksoap的工作方式。

所有的web服务,预计如果追加?wsdl到web服务端点来响应与WSDL。 ksoap期望这并请求WSDL。在这种情况下,webservice不会发回WSDL,并引发400错误。

你应该问的web服务提供商为什么WSDL不出来,当你为http://whatever.com/WebService/Request.asmx?wsdl发送请求。

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

+0

SOAP不是针对移动设备设计。对于简单的WSs,UrlConnection可以正常工作,但如果遇到复杂的问题,忘记UrlConnection即使是ksoap也不行。你将不得不使用REST。 – 2013-04-22 05:43:21

+0

@MehranMahmoudi如果你喜欢我的回答,为什么你不接受它? – 2013-04-22 20:11:07