开发Android应用程序并尝试解析新闻RSS提要但应用程序仍然失败。当xml以feed
开头但xml看起来像<rss><channel></channel></rss>
时,我可以解析RSS。我可以跟踪问题是在带有项目列表的通道类中,如果我添加required = false,则应用程序不会粉碎。这些是我解析RSS的类。
@Root(name = "rss")
public class Feed implements Serializable{
@Element(name = "channel")
private Channel mChannel;
public Channel getmChannel() {
return mChannel;
}
public Feed() {
}
public Feed(Channel mChannel) {
this.mChannel = mChannel;
}
@Override
public String toString() {
return "Feed{" +
"mChannel=" + mChannel +
'}';
}
} 频道类
@Root(name = "channel", strict = false)
public class Channel implements Serializable{
@ElementList(inline = true, name = "item")
public List<Item> items;
public Channel() {
}
public Channel(List<Item> items) {
this.items = items;
}
public List<Item> getItems() {
return items;
}
@Override
public String toString() {
return "Channel{" +
"items=" + items +
'}';
}
改造
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
//converter XML
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
FeedGitXivAPI feedGitXiv = retrofit.create(FeedGitXivAPI.class);
Call<Channel> call = feedGitXiv.getGitXiv();
这是logcat消息
onFailure: Unable to retrieve RSS: org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=true, name=item, required=true, type=void) on field 'items' public java.util.List com.example.anton.techy.AiMl.Channel.items for class com.example.anton.techy.AiMl.Channel at line -1