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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4416798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:16:57  来源:igfitidea点击:

Ksoap on android-- get a list response from a java web service

javaandroidweb-serviceslistandroid-ksoap2

提问by Hamzus

I have a java web service (soap) which i want to use with an android client for that am using ksoap.

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

My web service gives an answer which look like this :

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

<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>

wich is a list of object

这是一个对象列表

To call my web service i use this code :

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

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

When i tested my response "result" it's got only one object , how could i get all the list and parse it?

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

回答by Hamzus

The problem was that when i parse the soap response i got only the first object of the list so i changer this line :

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

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

with :

和 :

SoapObject result = (SoapObject)envelope.bodyIn;

wiht that i got all the list and i add this

有了所有列表,我添加了这个

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

    }

Good luck and thank you Janusz

祝你好运,谢谢你 Janusz

回答by maxy

Code:

代码:

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";

}

回答by Janusz

The result is a single SoapObjectthis objects however should have a property for every item in the list that you requested. You could do something like this to retrieve all the items:

结果是一个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;
}