1

我有一个 java web 服务 (soap),我想将它与一个使用 ksoap 的 android 客户端一起使用。

我的网络服务给出了一个如下所示的答案:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:listealbumResponse xmlns:ns2="http://ws/">
            <return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:album">
                <annee>2008</annee>
                <id>6</id>
                <titre>Ninja Tuna</titre>
            </return>
            <return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:album">
                <annee>2008</annee>
                <id>10</id>
                <titre>Fine Music, Vol. 1</titre>
            </return>
            <return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:album">
                <annee>2004</annee>
                <id>14</id>
                <titre>Bob Acri</titre>
            </return>
            <return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:album">
                <annee>2009</annee>
                <id>54</id>
                <titre>Rated R</titre>
            </return>
        </ns2:listealbumResponse>
    </S:Body>
</S:Envelope>

这是一个对象列表

要调用我的网络服务,我使用以下代码:

try{
        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;

        envelope.setOutputSoapObject(Request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        androidHttpTransport.call(SOAP_ACTION, envelope);


        SoapObject result = (SoapObject)envelope.getResponse();

当我测试我的响应“结果”时,它只有一个对象,我如何获取所有列表并解析它?

4

3 回答 3

4

问题是,当我解析肥皂响应时,我只得到了列表的第一个对象,所以我改变了这一行:

SoapObject result = (SoapObject)envelope.getResponse();

和 :

SoapObject result = (SoapObject)envelope.bodyIn;

我得到了所有的清单并添加了这个

testValues = new String[result.getPropertyCount()];
    for(int i= 0; i< result.getPropertyCount(); i++){
        testValues[i] = result.getProperty(i).toString(); 

    }

祝你好运,谢谢 Janusz

于 2010-12-11T18:33:31.677 回答
1

代码:

SoapObject result = (SoapObject)envelope.bodyIn;    
String output = "";    
for(int i= 0; i< result.getPropertyCount(); i++){
    SoapObject object = (SoapObject)response.getProperty(i);

    output += "annee : " + object.getProperty("annee") + "\n";
    output += "id : " + object.getProperty("id") + "\n";
    output += "titre : " + object.getProperty("titre") + "\n";

}
于 2011-12-14T01:51:54.873 回答
0

结果是一个SoapObject,但是这个对象应该对您请求的列表中的每个项目都有一个属性。您可以执行以下操作来检索所有项目:

private static List parseLists(List listItems, SoapObject response) {
    int propertyCount = response.getPropertyCount();
    for (int currentProperty = 0; currentProperty < propertyCount; currentProperty++) {
        Object input = response.getProperty(currentProperty);
        Object result = parseObject(input.toString());
        if (result != null) {
            listItems.add(result);
        }
    }
    return listItems;
}
于 2010-12-11T13:07:40.317 回答