改造和 SimpleXmlConverterFactory

Pas*_*Lzx 5 java xml android retrofit simple-xml-converter

我在改造和 XML 方面遇到问题,我使用 SimpleXmlConverterFactory,并且出现此错误: 原因为:“java.lang.IllegalArgumentException:无法找到 java.util.List 的 ResponseBody 转换器。尝试过:
*retrofit2.BuiltInConverters *retrofit2。转换器.simplexml.SimpleXmlConverterFactory”

等级:

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'
}
Run Code Online (Sandbox Code Playgroud)

服务 :

        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();
}
Run Code Online (Sandbox Code Playgroud)

模型 :

    @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() {}
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

小智 0

面临同样的问题。解决方案是将内部类设为静态(或者只是创建一个新的单独的 java 类)。还要为“通道”节点添加“Root”注释。

    @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;
  }

}
Run Code Online (Sandbox Code Playgroud)

与“enclosure”和“source”节点相同 - 创建新文件或创建静态内部类。

public class Enclosure {

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

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

    @Attribute(name = "type")
    private String type;
}
Run Code Online (Sandbox Code Playgroud)