android webservice响应“解析xml到pojo异常”

时间:2012-08-23 15:44:39

标签: android web-services xml-parsing

我正在尝试访问android中的soap webservices。

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

我将响应结果字符串作为xml格式,如下所示,

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

使用Simple XML Serialization(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;
    }
}

1 个答案:

答案 0 :(得分:0)

您的回复中存在问题。您将一个接一个地获得<clientNameList>两次,这可能会导致您在解析XML时遇到问题。因此,最好相应地更改您的回复。