Kei*_*irk 1 android kotlin simple-framework retrofit2
我正在尝试使用kotlin中的simplexml解析rss feed.
返回的数据具有以下结构:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Top Audio Podcasts</title>
<link>https://rss.itunes.apple.com/api/v1/us/podcasts/top-podcasts/all/10/explicit.rss</link>
<lastBuildDate>Fri, 13 Jul 2018 01:36:19 -0700</lastBuildDate>
<atom:link rel="self" type="application/rss+xml" href="https://rss.itunes.apple.com/api/v1/us/podcasts/top-podcasts/all/10/explicit.rss"/>
<atom:link rel="alternate" type="text/html" href="https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?genreId=26&popId=28"/>
<description>iTunes Store : Top Audio Podcasts</description>
<category>iTunes Store</category>
<copyright>Copyright © 2018 Apple Inc. All rights reserved.</copyright>
<item>
<title>The Wilderness - Crooked Media</title>
<category domain="">Crooked Media</category>
<link>https://itunes.apple.com/us/podcast/the-wilderness/id1408796715?mt=2</link>
<guid>https://itunes.apple.com/us/podcast/the-wilderness/id1408796715?mt=2</guid>
<description>The Wilderness</description>
<category>podcast</category>
<category>News & Politics</category>
<category>Podcasts</category>
<category>Society & Culture</category>
<category>History</category>
<pubDate>Fri, 6 Jul 2018 00:00:00 +0000</pubDate>
</item>
<item>
<title>Aaron Mahnke's Cabinet of Curiosities - HowStuffWorks & Aaron Mahnke</title>
<category domain="https://itunes.apple.com/us/artist/howstuffworks-aaron-mahnke/284341002?mt=2">HowStuffWorks & Aaron Mahnke</category>
<link>https://itunes.apple.com/us/podcast/aaron-mahnkes-cabinet-of-curiosities/id1396546917?mt=2</link>
<guid>https://itunes.apple.com/us/podcast/aaron-mahnkes-cabinet-of-curiosities/id1396546917?mt=2</guid>
<description>Aaron Mahnke's Cabinet of Curiosities</description>
<category>podcast</category>
<category>History</category>
<category>Podcasts</category>
<category>Society & Culture</category>
<pubDate>Tue, 10 Jul 2018 00:00:00 +0000</pubDate>
</item>
</channel>
</rss>
Run Code Online (Sandbox Code Playgroud)
我关心的部分是项目标签包含的播客列表.
模型类我看起来像这样:
@Root(name = "channel", strict = false)
data class Chart @JvmOverloads constructor(
@field:ElementList(entry = "item", inline = true)
@param:ElementList(entry = "item", inline = true)
val podcasts: List<Entry>? = null)
@Root(name = "item", strict = false)
data class Entry @JvmOverloads constructor(
@field:Element(name = "description")
@param:Element(name = "description")
val title: String? = null,
@field:Element(name = "category")
@param:Element(name = "category")
val artist: String? = null,
@field:Element(name = "guid")
@param:Element(name = "guid")
val guid: String? = null)
Run Code Online (Sandbox Code Playgroud)
我有一个简单的单元测试,从文件加载xml,将其传递给simplexml反序列化器,然后将输出与一些预期的模型进行比较.
在我运行测试的那一刻,我得到一个例外:
org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(entry=item, inline=true, data=false, name=, type=void, required=true, empty=true) on field 'podcasts' private final java.util.List Chart.podcasts for class Chart at line 2
Run Code Online (Sandbox Code Playgroud)
向@ElementList注释添加required = false会删除此错误,但反序列化器会为列表返回null.
我最好的猜测是反序列化器无法找到项目列表,尽管根据我对@ElementList的理解,使用输入字段应该允许它找到标签.
此代码的用例是形成Android应用程序的网络层,该应用程序依赖于Retrofit2进行REST调用.
感谢您提供的任何帮助.
注意:使用@Param和@Field声明是为了解决另一个异常,我从这个问题中得到了一个想法
我找到了解决方案.
所以第一个问题是我的根元素不是Chart,因为它代表了channel标签,我需要一个包装类,我称之为ChartFeed来表示rss标签.
我通过在Chart对象上添加title标签的元素来实现这一点,这也是不可行的.
我完成的数据类如下所示:
@Root(name = "rss", strict = false)
data class ChartFeed(
@field:Element(name = "channel")
@param:Element(name = "channel")
val chart: Chart? = null)
@Root(name = "channel", strict = false)
data class Chart @JvmOverloads constructor(
@field:ElementList(entry = "item", inline = true)
@param:ElementList(entry = "item", inline = true)
val podcasts: List<Entry>? = null)
@Root(name = "item", strict = false)
data class Entry @JvmOverloads constructor(
@field:Path("description")
@field:Text(required = false)
@param:Path("description")
@param:Text(required = false)
val title: String? = null,
@field:Path("category")
@field:Text(required = false)
@param:Path("category")
@param:Text(required = false)
val artist: String? = null,
@field:Element(name = "guid")
@param:Element(name = "guid")
val guid: String? = null)
Run Code Online (Sandbox Code Playgroud)
我已经将@Element标记的几个字段/参数更改为@Path和@Text的组合,这是因为channel和item xml对象都重用了导致此异常的相同标记:
org.simpleframework.xml.core.PersistenceException: Element 'category' is already used with @org.simpleframework.xml.Element(name=category, type=void, data=false, required=true) on field 'artist' private final java.lang.String Entry.artist at line 18
Run Code Online (Sandbox Code Playgroud)
我使用的解决方案在此处详述
| 归档时间: |
|
| 查看次数: |
1187 次 |
| 最近记录: |