【问题标题】:android webservice response "parse xml to pojo exception"android webservice响应“解析xml到pojo异常”
【发布时间】:2012-08-19 04:38:57
【问题描述】:

我正在尝试访问 android 中的肥皂网络服务。

        AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
        ...
        ...
        String result = (String) httpTransport.responseDump;

我得到了下面给出的xml格式的响应result字符串,

<?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <soapenv:Body>
            <sampleResponse xmlns="http://impl.test.com">
                <sampleReturn>
                    <clientNameList>
                        <clientNameList>
                            <clientID>1</clientID>
                        </clientNameList>
                        <clientNameList>
                            <clientID>2</clientID>
                        </clientNameList>
                    </clientNameList>
                    <message>SUCCESS</message>
                </sampleReturn>
            </sampleResponse>
        </soapenv:Body>
    </soapenv:Envelope>

使用简单 XML 序列化 (simple-xml-2.6.6.jar) 将此 xml 解析为 Pojo。 参考: here

        Persister persister = new Persister();
        UserResponse userResponse = persister.read(UserResponse.class, result);

现在我得到了例外

Element 'Body' does not have a match in class com.test.UserResponse at line 1

更多代码信息here

我该如何解决这个问题?

UserResponse.java(POJO 类)

public class UserResponse {

private String message = null;

private Client[] clientNameList = null;

public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

public void setClientNameList(Client[] clientNameList) {
        this.clientNameList = clientNameList;
    }

    public Client[] getClientNameList() {
        return clientNameList;
    }
}

和Client.java

public class Client {

private int clientID;

public void setClientID(int clientID) {
        this.clientID = clientID;
    }

    public int getClientID() {
        return clientID;
    }
}

【问题讨论】:

  • 请向我们展示您创建的 POJO 类。
  • 我在问题中添加了我的 POJO 类

标签: android web-services xml-parsing


【解决方案1】:

您的回复有问题。您一次又一次地收到&lt;clientNameList&gt; 两次,这可能会导致您在解析 XML 时出现问题。所以,最好相应地改变你的反应。

【讨论】:

    猜你喜欢
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 2012-03-11
    • 2012-05-14
    • 1970-01-01
    相关资源
    最近更新 更多