我正在尝试了解如何在Android上使用a SOAP webservice (link to wsdl)。我有一个简单的方法,将ksoap2 3.0与依赖项jar一起使用:
public void simpleSoap() {
String SOAP_ACTION = "";
String METHOD_NAME = "getElement";
String NAMESPACE = "http://www.wcc.nrcs.usda.gov/ns/awdbWebService";
String URL = "http://www.wcc.nrcs.usda.gov/awdbWebService/services?WSDL";
try {
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("elementCd","WTEQ");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE transport = new HttpTransportSE(URL);
transport.call(SOAP_ACTION, soapEnvelope);
resultString = (SoapPrimitive) soapEnvelope.getResponse();
Log.i(TAG, "Result: " + resultString);
} catch (Exception ex) {
Log.e(TAG, "Error: " + ex.getMessage());
}
}
每次响应时我都会收到错误/强制关闭:
Error: Unmarshalling Error: unexpected element
(uri:"http://www.wcc.nrcs.usda.gov/ns/awdbWebService",
local:"elementCd"). Expected elements are <{}elementCd>
我已经在wonderful online client上对此进行了测试,并获得了结果。我在这里想念什么?为什么我无法将此硬编码到addProperty函数中的简单elementCd WTEQ得到结果?
[三周后,我偶然发现了这个问题:https://stackoverrun.com/es/q/2805758
这是因为期望的名称空间必须为空字符串。因此,在将属性添加到请求时,只需确保在名称空间中放入“”即可。
Request.addProperty("", "elementCd","WTEQ");