I'm trying to use Retrofit with SimpleXmlConverter to read data from my API.
My class Comment looks like:
@Root
class Comment {
@Element
private String text;
}
I would like to read list of comments from XML:
<comments>
<comment>
<text>sample text</text>
</comment>
<comment>
<text>sample text</text>
</comment>
</comments>
In my interface there is a method:
@GET("/lastcomments")
ArrayList<Comment> lastComments();
but when I call lastComments() Retrofit throws:
Caused by: retrofit.RetrofitError: java.lang.ClassCastException: libcore.reflect.ParameterizedTypeImpl cannot be cast to java.lang.Class
...
Caused by: retrofit.converter.ConversionException: java.lang.ClassCastException: libcore.reflect.ParameterizedTypeImpl cannot be cast to java.lang.Class
at com.mobprofs.retrofit.converters.SimpleXmlConverter.fromBody(SimpleXmlConverter.java:76)
...
Caused by: java.lang.ClassCastException: libcore.reflect.ParameterizedTypeImpl cannot be cast to java.lang.Class
at com.mobprofs.retrofit.converters.SimpleXmlConverter.fromBody(SimpleXmlConverter.java:72)
Is there possible to read list directly from API or I have to create wrapper:
@Root(name="comments")
class CommentsList {
@Element(name="comment", inline=true)
List<Comment> comments;
}
Sorry I know this is probably too late but here is the answer:
You need to use an ElementList attribute:
@Root(name="comments")
class CommentsList {
@ElementList(name="comment")
List<Comment> comments;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With