android webservice响应“解析xml到pojo异常" [英] android webservice response "parse xml to pojo exception"
问题描述
我正在尝试访问 android 中的 soap 网络服务.
I am trying to access soap webservices in android.
AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
...
...
String result = (String) httpTransport.responseDump;
我得到了响应 result 字符串作为下面给出的 xml 格式,
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>
使用简单 XML 序列化 (simple-xml-2.6.6.jar) 将此 xml 解析为 Pojo.参考: 这里>
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);
现在我得到了异常
Element 'Body' does not have a match in class com.test.UserResponse at line 1
有关更多代码信息此处
我该如何解决这个问题?
How can I fix this?
UserResponse.java(POJO 类)
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;
}
}
和 Client.java
and Client.java
public class Client {
private int clientID;
public void setClientID(int clientID) {
this.clientID = clientID;
}
public int getClientID() {
return clientID;
}
}
推荐答案
您的回复有问题.您一次又一次地获得
两次,因此可能会导致您在解析 XML 时出现问题.因此,最好相应地更改您的回复.
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.
这篇关于android webservice响应“解析xml到pojo异常"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!