Android 上的 Ksoap——从 Java Web 服务获取列表响应

发布于 2024-10-07 10:46:05 字数 2031 浏览 7 评论 0原文

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

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

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

じее 2024-10-14 10:46:05

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

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

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

临走之时 2024-10-14 10:46:05

代码:

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

}

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

}
梦里南柯 2024-10-14 10:46:05

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

The result is a single SoapObject this 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:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文