我正在尝试从以下XML文件实现绑定的基类。
<BaseClass>
<SUCCESS>TRUE</SUCCESS>
<MESSAGE>TEST</MESSAGE>
<CONTENT>
<InfoList>
<Info>
<MESSAGE>Test1</MESSAGE>
</Info>
<Info>
<MESSAGE>Test2</MESSAGE>
</Info>
</InfoList>
</CONTENT>
<BaseClass/>
主要活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Serializer serializer = new Persister();
try {
BaseClass<InfoList> contact = serializer.read(BaseClass.class, readAsset(getAssets(), "textXml.xml"));
Log.v("Information", contact.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
BaseClass.java
@Root
public class BaseClass<T> {
@Element
private String SUCCESS;
@Element
private String MESSAGE;
@Element
T CONTENT;
public String getSUCCESS() {
return SUCCESS;
}
public void setSUCCESS(String SUCCESS) {
this.SUCCESS = SUCCESS;
}
public String getMESSAGE() {
return MESSAGE;
}
public void setMESSAGE(String MESSAGE) {
this.MESSAGE = MESSAGE;
}
public T getCONTENT() {
return CONTENT;
}
public void setCONTENT(T CONTENT) {
this.CONTENT = CONTENT;
}
}
InfoList.java:
@Root
public class InfoList {
@ElementList(type = Info.class)
private List<Info> infoList;
public List<Info> getInfoList() {
return infoList;
}
public void setInfoList(List<Info> infoList) {
this.infoList = infoList;
}
}
信息:
@Root
public class Info {
@Element
private String MESSAGE;
public String getMESSAGE() {
return MESSAGE;
}
public void setMESSAGE(String MESSAGE) {
this.MESSAGE = MESSAGE;
}
}
XML示例是HTTP结果的答案,除了CONTENT部分之外,它是每个结果的标准。我想实现该类,以便您可以反序列化XML并将其绑定到实例BaseClass&lt;%Your class%&gt;这样它就可以是通用的,并且可以摆脱每个结果的标准绑定。
当我尝试没有T类型的代码并将InfoList类型写入CONTENT时,它工作正常,但每次我尝试代码时,反序列化器将T类型视为Object并且它给出了异常,因此无法找到InfoList参数在Object类中。