使用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>
我可以通过设置信封.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>
我需要做什么才能获得相同的 XML,仅在 xmlns 属性上具有“n0”前缀,如下所示
<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 标头的 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.