在Retrofit中使用SimpleXMLConverter将XML响应转换为POJO

时间:2017-03-31 15:04:19

标签: android xml rx-java retrofit2 simple-xml-converter

我遇到了以下错误。 这是XML响应

<rss xmlns:content="http://purl.org/rss/1.0/modules/content/"     xmlns:dcterms="http://purl.org/dc/terms/"  xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        <title>SnagFilms Recent Additions</title>
        <item>
            <media:title>Sink or Swim - Trailer</media:title>
            <title>Sink or Swim - Trailer</title>
            <media:description> Jon Bowermaster's documentary on a learn-to-swim camp for third graders and their moms on the island-nation of the Maldives
            </media:description>
            <media:credit role="Director" scheme="urn:ebu">Jon Bowermaster</media:credit>
            <media:thumbnail type="landscape" url="http://snagfilms-a.akamaihd.net/sinkorswim-video.jpg"/>
            <media:content duration="117" height="323"  width="500"/>
            <media:rights status="official"/>
            <media:player height="323" url="http://embed.snagfilms.com/embed/player?filmId=00000158-b20c-d8f9-affd-b32ce8700000" width="500"/>
         </item>
    </channel>
</rss>

这是我使用可观察

的Rest Call代码
rx.Observable<Rss> videosObservable = RetrofitHelper.createListObs();
    videosObservable.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<Rss>() {
                @Override
                public void onCompleted() {

                    Log.d(TAG, "onCompleted: ");
                }

                @Override
                public void onError(Throwable e) {
                    Log.d(TAG, "onError: " + e.toString());
                }

                @Override
                public void onNext(Rss rss) {
                    Log.d(TAG, "onNext: " );

                }
            });

这是我得到的错误

onError: java.lang.RuntimeException: org.simpleframework.xml.core.ElementException: Element 'item' does not have a match in class com.example.singh.xmlParser.model.Channel at line 12

我的Rss课程

@Root(strict = false)
public class Rss {

public Channel getChannel() {
    return channel;
}
@Element
public Channel channel;

}

我的频道类

@Element
public class Channel {

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


@ElementList
List<Item> items;

public List getItems(){
    return items;
}

}

我需要帮助在Java中为XML创建适当的POJO。如果有人可以为给定的格式制作所有POJO,我将非常感激。回复链接http://www.snagfilms.com/feeds/。转换器文档http://simple.sourceforge.net/home.php

1 个答案:

答案 0 :(得分:0)

item的列表是channel对象中的内联列表。你必须在你的注释中指明:

@ElementList(inline=true)
List<Item> items;

检查official documentation

相关问题