Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading List<Item> with Retrofit from XML API

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;
}
like image 718
user3608167 Avatar asked Jan 07 '15 10:01

user3608167


1 Answers

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;
}
like image 128
Quintin Balsdon Avatar answered Oct 08 '22 19:10

Quintin Balsdon