简体   繁体   中英

Retrofit and SimpleXmlConverterFactory

I have a problem with retrofit and XML, I use SimpleXmlConverterFactory, and I have this error : Caused by: "java.lang.IllegalArgumentException: Could not locate ResponseBody converter for java.util.List. Tried:
* retrofit2.BuiltInConverters * retrofit2.converter.simplexml.SimpleXmlConverterFactory"

gradle:

compile 'com.google.code.gson:gson:2.8.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'
compile ('com.squareup.retrofit2:converter-simplexml:2.3.0') {
    exclude group: 'xpp3', module: 'xpp3'
    exclude group: 'stax', module: 'stax-api'
    exclude group: 'stax', module: 'stax'
}

service :

        Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(URL)
            .client(buildHttpClient())
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .build();

private OkHttpClient buildHttpClient() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    return (new OkHttpClient.Builder())
            .addInterceptor(buildDefaultHeadersInterceptor())
            .addNetworkInterceptor(buildLogInterceptor())
            .build();
}

model :

    @Root(name = "item")
    public class Item {

    @Element(name = "link")
    private String link;

    @Element(name = "title")
    private String title;

    @Element(name = "description")
    private String description;

    public Item() {}
}

xml :

<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
   <channel xml:lang="fr">
       <title>...</title>
       <link>http://www.jouars-pontchartrain.fr/</link>
       <description>...</description>
       <language>fr</language>
       <generator>SPIP - www.spip.net</generator>
       <image>...</image>
       <item xml:lang="fr">
           <title>
              title here
           </title>
           <link>
              link here
           </link>
           <description>
              description here
           </description>
       </item>
       <item xml:lang="fr">...</item>
       <item xml:lang="fr">...</item>
       <item xml:lang="fr">...</item>
       <item xml:lang="fr">...</item>
   </channel>
</rss>

Faced with the same problem. The solution is to make the internal class static (or just create a new separate java class). Also add the "Root" annotation for the "channel" node.

    @Root(name = "rss", strict = false)
public class ArticleResponse {

@Element(name = "channel")
private Channel channel;


@Root(name = "channel", strict = false)
static class Channel {

    @ElementList(inline = true, name="item")
    private List<Article> articles;
  }

}

The same with the "enclosure" and "source" nodes - create new files or create static internal classes.

public class Enclosure {

    @Attribute(name = "url")
    private String url;

    @Attribute(name = "length")
    private long length;

    @Attribute(name = "type")
    private String type;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM