使用 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>
我需要做什么才能像这样在 xmlns 属性上获得带有“n0”前缀的相同 XML
<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 使用带有 Http header 的 HttpClient 进行连接,并发布来自 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.
最佳答案
一些事情 1st 下面是与 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();
关于android - 仅在 xmlns 属性上设置 KSOAP2 前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12634082/