Android Retrofit 2简单XML转换器

前端之家收集整理的这篇文章主要介绍了Android Retrofit 2简单XML转换器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Retrofit 2.1.0和Retrofit Simple XML Converter 2.1.0.我添加了simplexmlconverter来使用addConverterFactory方法改进实例.

XML如下

<?xml version="1.0" encoding="UTF-8"?>
<RSS xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mRSS/" version="2.0">
   <channel>
      <title>title</title>
      <description></description>
      <language>en-us</language>
      <item>
         <title>text</title>
         <link>text</link>
         <description>text</description>
         <enclosure url="text" length="2043520" type="image/jpeg" />
         <guid isPermaLink="false">text</guid>
         <pubDate>Fri,17 Jun 2016 11:43 EDT</pubDate>
         <source url="text">text</source>
      </item>
      <item>
         <title>text</title>
         <link>text</link>
         <description>text</description>
         <enclosure url="text" length="1735257" type="image/jpeg" />
         <guid isPermaLink="false">text</guid>
         <pubDate>Thu,16 Jun 2016 10:17 EDT</pubDate>
         <source url="text"></source>
      </item>
      <item>
         <title>text</title>
         <link>text</link>
         <description>text</description>
         <enclosure url="text" length="3763157" type="image/jpeg" />
         <guid isPermaLink="false">text</guid>
         <pubDate>Wed,15 Jun 2016 10:02 EDT</pubDate>
         <source url="text">text</source>
      </item>
   </channel>
</RSS>

我的改造api客户端相关代码:RetrofitAPIClient

OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(loggingInterceptor)
            .build();

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

apiService = retrofit.create(MyService.class);

ArticleResponse.java

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import java.util.List;

@Root(name = "RSS")
public class ArticleResponse {

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

    public class Channel {

        @ElementList
        public List<Article> articles;
    }
}

Article.java

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Text;

@Element(name = "item")
public class Article {

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

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

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

    @Element(name = "enclosure")
    private Enclosure enclosure;

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

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

    @Element(name = "source")
    private Source source;

    public class Enclosure {

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

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

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

    public class Source {

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

        @Text
        private String text;
    }
}

错误是:

06-18 20:31:22.894 W/System.err: java.lang.RuntimeException: org.simpleframework.xml.core.AttributeException: Attribute 'version' does not have a match in class [my-package].webservice.response.ArticleResponse at line 1
06-18 20:31:22.894 W/System.err:     at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:44)
06-18 20:31:22.894 W/System.err:     at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:23)
06-18 20:31:22.894 W/System.err:     at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:116)
06-18 20:31:22.894 W/System.err:     at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
06-18 20:31:22.894 W/System.err:     at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:106)
06-18 20:31:22.894 W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
06-18 20:31:22.894 W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
06-18 20:31:22.894 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-18 20:31:22.894 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-18 20:31:22.894 W/System.err:     at java.lang.Thread.run(Thread.java:818)
06-18 20:31:22.894 W/System.err: Caused by: org.simpleframework.xml.core.AttributeException: Attribute 'version' does not have a match in class [my-package].webservice.response.ArticleResponse at line 1
06-18 20:31:22.895 W/System.err:     at org.simpleframework.xml.core.Composite.readAttribute(Composite.java:494)
06-18 20:31:22.895 W/System.err:     at org.simpleframework.xml.core.Composite.readAttributes(Composite.java:413)
06-18 20:31:22.895 W/System.err:     at org.simpleframework.xml.core.Composite.access$300(Composite.java:59)
06-18 20:31:22.895 W/System.err:     at org.simpleframework.xml.core.Composite$Builder.read(Composite.java:1382)
06-18 20:31:22.895 W/System.err:     at org.simpleframework.xml.core.Composite.read(Composite.java:201)
06-18 20:31:22.895 W/System.err:     at org.simpleframework.xml.core.Composite.read(Composite.java:148)
06-18 20:31:22.895 W/System.err:     at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)
06-18 20:31:22.895 W/System.err:     at org.simpleframework.xml.core.Persister.read(Persister.java:625)
06-18 20:31:22.895 W/System.err:     at org.simpleframework.xml.core.Persister.read(Persister.java:606)
06-18 20:31:22.895 W/System.err:     at org.simpleframework.xml.core.Persister.read(Persister.java:584)
06-18 20:31:22.895 W/System.err:     at org.simpleframework.xml.core.Persister.read(Persister.java:543)
06-18 20:31:22.895 W/System.err:     at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:36)
06-18 20:31:22.895 W/System.err:    ... 9 more

解决方法

尝试使用strict = false:
@Root(name = "RSS",strict = false)
public class ArticleResponse {

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

    public class Channel {

        @ElementList
        public List<Article> articles;
    }
}

猜你在找的Android相关文章