android webservice response “parse xml to pojo exception”

有些话、适合烂在心里 提交于 2019-12-23 17:11:08

问题


I am trying to access soap webservices in android.

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

I got the response result string as xml format given below,

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

Parsing this xml into Pojo using Simple XML Serialization(simple-xml-2.6.6.jar). Ref: here

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

Now I got the exception as

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

For more code information here

How can I fix this?

UserResponse.java(POJO class)

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

and Client.java

public class Client {

private int clientID;

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

    public int getClientID() {
        return clientID;
    }
}

回答1:


There is a problem in your response. You are getting <clientNameList> two times one after another so that might be causing you a problem in parsing the XML. So, better change your response accordingly.



来源:https://stackoverflow.com/questions/12095437/android-webservice-response-parse-xml-to-pojo-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!