Android Retrofit 2 Simple XML Converter
Solution 1
Try with strict = false
:
@Root(name = "rss", strict = false)
public class ArticleResponse {
@Element(name = "channel")
public Channel channel;
public class Channel {
@ElementList
public List<Article> articles;
}
}
Solution 2
The reason you're seeing this is because you're missing the annotation for the "default" attribute in your XML document.
SimpleXML's default behavior is to set (strict = true), which means that you need to annotate ALL elements and attributes in your document, or it will throw the runtime exception you're seeing.
So, for the sample XML below:
<response xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XML-Schema-instance" version="1.2">
<sample_string>Brady</sample_string>
<sample_integer>12</sample_integer>
</response>
You would either need to use the (strict = false) flag to ignore the "version" attribute:
@Root(strict = false)
public class Response {
@Element(name = "sample_string")
private String sampleString;
@Element(name = "sample_integer")
private Integer sampleInteger;
public String getSampleString() {
return sampleString;
}
public Integer getSampleInteger() {
return sampleInteger;
}
}
OR, annotate the "version" attribute in your class:
@Root
public class Response {
@Attribute(name = "version")
private String version;
@Element(name = "sample_string")
private String sampleString;
@Element(name = "sample_integer")
private Integer sampleInteger;
public String getVersion() {
return version;
}
public String getSampleString() {
return sampleString;
}
public Integer getSampleInteger() {
return sampleInteger;
}
}
Comments
-
okarakose almost 2 years
I am using Retrofit 2.1.0 and Retrofit SimpleXML Converter 2.1.0. I added simplexmlconverter to retrofit instance with addConverterFactory method.
XML is below
<?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>
My retrofit api client relevant code : 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; } }
Error is :
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