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 106-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.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 106-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.access0(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 Retrofit 2简单XML转换器全部内容,希望文章能够帮你解决Android Retrofit 2简单XML转换器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)