Android上的Ksoap--从java Web服务获取列表响应

时间:2010-12-11 12:55:56

标签: java android web-services list android-ksoap2

我有一个java web服务(soap),我想用一个Android客户端来使用kso​​ap。

我的网络服务给出的答案如下:

<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();

当我测试我的回答“结果”时,它只有一个对象,我怎么能得到所有列表并解析它?

3 个答案:

答案 0 :(得分:3)

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

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

with:

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

答案 1 :(得分: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";

}

答案 2 :(得分: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;
}