1

使用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.
4

1 回答 1

1

下面是与 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();
于 2012-09-29T03:46:13.763 回答