2015-01-07 7 views
5

Ich versuche Retrofit mit SimpleXmlConverter zu verwenden, um Daten von meiner API zu lesen.Leseliste <Item> mit Retrofit von XML API

Meine Klasse Kommentar wie folgt aussieht:

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

Ich möchte Liste Kommentare von XML lesen:

@GET("/lastcomments") 
ArrayList<Comment> lastComments(); 

:

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

In meiner Schnittstelle gibt es ein Verfahren aber wenn ich lastComments() rufe Retrofit wirft:

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) 

Gibt es möglich Liste direkt von API zu lesen oder ich habe Wrapper erstellen:

@Root(name="comments") 
class CommentsList { 
    @Element(name="comment", inline=true) 
    List<Comment> comments; 
} 

Antwort

2

Sie benötigen CommentList Klasse. Die Schnittstelle sollte sein:

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

für synchrone Anrufe oder

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

für asynchrone Aufrufe.

5

Sorry, ich weiß, das ist wahrscheinlich zu spät ist, aber hier ist die Antwort:

Sie benötigen ein Elementliste Attribut verwenden:

@Root(name="comments") 
class CommentsList { 
    @ElementList(name="comment") 
    List<Comment> comments; 
} 
Verwandte Themen