使用 KSOAP2 ,我可以在SOAP信封中生成此XML:
<v:Envelope
xmlns:i="http://www.w3.org/1999/XMLSchema-instance"
xmlns:d="http://www.w3.org/1999/XMLSchema"
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
<ApplyItem xmlns="http://www.myurl.com/">
<Item type="User" action="get" select="login_name"/>
</ApplyItem>
</v:Body>
</v:Envelope>
我可以通过设置envelope.dotNet = false;
来获取此XML<v:Envelope
xmlns:i="http://www.w3.org/1999/XMLSchema-instance"
xmlns:d="http://www.w3.org/1999/XMLSchema"
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
<n0:ApplyItem xmlns:n0="http://www.myurl.com/">
<n0:Item type="User" action="get" select="login_name"/>
</n0:ApplyItem>
</v:Body>
</v:Envelope>
我需要做什么才能获得与&#39; n0&#39;相同的XML。仅在xmlns属性上添加前缀
<ApplyItem xmlns:n0="http://www.myurl.com/">
<Item type="User" action="get" select="login_name" />
</ApplyItem>
我遵循的代码
public void TestWebService() {
/*xmlVersionTag = "";
* NAMESPACE = "http://www.myurl.com/";
* SOAPaction = "ApplyItem";
* MYSERVER = "http://myServer/webservice.aspx";
* DATABASE = "dbName";
* AUTHUSER = "admin";
* AUTHPASSWORD = "pwd"; */
SoapObject request = new SoapObject(NAMESPACE, SOAPaction);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setAddAdornments(false);
envelope.implicitTypes = true;
envelope.addMapping(null, "Item", new String().getClass());
SoapObject aml = new SoapObject(NAMESPACE, "Item");
aml.addAttribute("type", "User");
aml.addAttribute("action", "get");
aml.addAttribute("select", "login_name");
request.addSoapObject(aml);
envelope.setOutputSoapObject(request);
HttpClient client = new DefaultHttpClient();
try {
callWS(client, MYSERVER, SOAPaction, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
Log.i("Webservice Output", response.toString());
} catch (Exception e) {
e.printStackTrace();
}
return;
}
CallWS使用HttpClient与Http标头连接,并从soap信封发布请求数据,并将响应解析回信封输入。
这是代码的一部分。
public void callAras(HttpClient httpClient,
String url, String soapAction, SoapEnvelope envelope)
throws IOException, XmlPullParserException {
if (soapAction == null) {
soapAction = "\"\"";
}
byte[] requestData = createRequestData(envelope);
String requestDump = new String(requestData);
HttpPost method = new HttpPost(url);
method.addHeader("SOAPAction", soapAction);
method.addHeader("AUTHUSER", AUTHUSER);
method.addHeader("AUTHPASSWORD", AUTHPASSWORD);
method.addHeader("DATABASE", DATABASE);
HttpEntity entity = new ByteArrayEntity(requestData);
method.setEntity(entity);
HttpResponse response = httpClient.execute(method);
InputStream inputStream = response.getEntity().getContent();
parseResponse(envelope, inputStream);
inputStream.close();
}
我收到的错误代码是:
SOAP-ENV:Server.TagItemIsNotFoundInRequestException
There is no tag <Item> in request.
答案 0 :(得分:0)
首先,与Web服务一起使用的请求XML如下所示。请注意,'n0'前缀已从Item元素中删除。
<v:Envelope
xmlns:i="http://www.w3.org/1999/XMLSchema-instance"
xmlns:d="http://www.w3.org/1999/XMLSchema"
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
<n0:ApplyItem xmlns:n0="http://www.myurl.com/">
<Item type="User" action="get" select="login_name"/>
</n0:ApplyItem>
</v:Body>
</v:Envelope
我犯的错误是使用SoapObject来定义Item元素,而不是使用SoapPrimitive类来定义它。代码更正如下:
envelope.dotNet = false;
envelope.setAddAdornments(false);
envelope.implicitTypes = true;
SoapPrimitive amlItem = new SoapPrimitive(NAMESPACE, "Item", "");
amlItem.addAttribute("type", "User");
amlItem.addAttribute("action", "get");
amlItem.addAttribute("select", "login_name");
request.addProperty("Item", amlItem);
envelope.setOutputSoapObject(request);
请求一旦工作,我需要更改来自
的响应的强制转换SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
到下面处理KSOAP返回的'anytype'。
Object response = (Object) envelope.getResponse();