<Item> Reading List with Update from XML API

I am trying to use Retrofit with SimpleXmlConverter to read data from my API.

My Comment class is as follows:

@Root class Comment { @Element private String text; } 

I would like to read a list of comments from XML:

 <comments> <comment> <text>sample text</text> </comment> <comment> <text>sample text</text> </comment> </comments> 

There is a method in my interface:

 @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 it possible to read the list directly from the API or do I need to create a wrapper:

 @Root(name="comments") class CommentsList { @Element(name="comment", inline=true) List<Comment> comments; } 
+5
source share
2 answers

Sorry, I know this is probably too late, but here is the answer:

You need to use the ElementList attribute:

 @Root(name="comments") class CommentsList { @ElementList(name="comment") List<Comment> comments; } 
+5
source

You need to use your CommentList class. The interface should be:

 @GET("/lastcomments") CommentList lastComments(); 

for synchronous calls or

 @GET("/lastcomments") void lastComments(Callback<CommentList> callback); 

for asynchronous calls.

+2
source

Source: https://habr.com/ru/post/1210539/


All Articles