2

I'm trying to implement a base class for binding from the following XML file.

<BaseClass>
    <SUCCESS>TRUE</SUCCESS>
    <MESSAGE>TEST</MESSAGE>
    <CONTENT>
        <InfoList>
            <Info>
                <MESSAGE>Test1</MESSAGE>
            </Info>
            <Info>
                <MESSAGE>Test2</MESSAGE>
            </Info>
        </InfoList>
    </CONTENT>
<BaseClass/>

Main Activity:

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

Information:

@Root
public class Info {
    @Element
    private String MESSAGE;

    public String getMESSAGE() {
        return MESSAGE;
    }

    public void setMESSAGE(String MESSAGE) {
        this.MESSAGE = MESSAGE;
    }
}

The XML example is an answer of an HTTP result, and it is standard for every result except for the CONTENT part. I want to implement the class such that you can deserialise XML and bind it to the instance BaseClass<%Your class%> so that it can be generic and get rid of standard binding for every result.

When I try the code without the T type and write InfoList type to CONTENT it works fine, but each time I try the code, the deserialiser sees the T type as Object and it gives exception such that no InfoList parameter can be found in the Object class.

8

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.