2016-06-18 60 views
5

Sto usando Retrofit 2.1.0 e Retrofit SimpleXML Converter 2.1.0. Ho aggiunto simplexmlconverter per aggiornare l'istanza con il metodo addConverterFactory.Android Retrofit 2 Simple XML Converter

XML è al di sotto

<?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> 

mio retrofit client API codice rilevante: 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; 
    } 
} 

errore è:

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 
+0

Eventuali aggiornamenti su questo? –

+0

@AmilcarAndrade, controlla la mia risposta. –

risposta

3

Prova con strict = false:

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

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

    public class Channel { 

     @ElementList 
     public List<Article> articles; 
    } 
} 
0

Di fronte a un medesimo problema. La soluzione è rendere statiche le classi interne (o semplicemente creare una nuova singola classe java). Aggiungi anche l'annotazione "Root" per il nodo "canale".

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

} 

Uguale ai nodi "enclosure" e "source": creare nuovi file o creare classi interne statiche.

public class Enclosure { 

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

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

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

Il motivo per cui viene visualizzato questo è perché manca l'annotazione per l'attributo "predefinito" nel documento XML.

Il comportamento predefinito di SimpleXML è impostare (strict = true), il che significa che è necessario annotare TUTTI gli elementi e gli attributi nel documento oppure genererà l'eccezione di runtime visualizzata.

Così, per l'XML di esempio riportato di seguito:

<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> 

Si sarebbe sia bisogno di utilizzare il (rigoroso = false) bandiera di ignorare la "versione" attributo:

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

O, annotare l'attributo "version" nella classe:

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