【发布时间】:2018-05-30 11:06:26
【问题描述】:
我正在使用 SimpleXmlConverterFactory 和 Retrofit2。以下是我尝试解析的示例响应:
<entries timestamp="1513178530.8394">
<entry id="2" date="20170104">
<fruits>
<fruit type="apple" count="16"/>
<fruit type="banana" count="12"/>
<fruit type="cerry" count="5"/>
<fruit type="lemon" count="2"/>
<fruit type="orange" count="2"/>
<fruit type="pear" count="0"/>
<fruit type="pineapple" count="2"/>
</fruits>
</entry>
<entry id="21" date="20170306">
<fruits>
<fruit type="pear" count="1"/>
<fruit type="orange" count="3"/>
<fruit type="banana" count="1"/>
<fruit type="cerry" count="1"/>
<fruit type="apple" count="2"/>
</fruits>
</entry>
</entries>
现在我使用下面的类来解析:
@Root(name = "entries")
public class Entries {
@Attribute(name = "timestamp")
private String timestamp;
@ElementList(name = "entry")
private List<EntryLog> entry;
}
@Root(name = "entry")
class Entry {
@Element(name = "fruits",required = false)
private Fruits fruits;
}
@Root(name = "fruits")
class Fruits{
@ElementList(name = "fruit")
private List<FruitItem> fruit;
}
@Root(name = "fruit")
class Fruit {
@Attribute(name = "type")
private String type;
@Attribute(name = "count")
private int count;
}
我似乎无法让它工作,错误说:
org.simpleframework.xml.core.ElementException: Element 'fruit' 确实 在课堂上没有比赛 com.github.irshulx.fruitdiary.apps.main.model.EntryLog 在第 -1 行
这个错误没有意义。因为fruit不属于EntryLog。
非常感谢任何帮助。
【问题讨论】:
-
也许你需要用水果根元素注释你的
FruitItem类? -
试过了,还是一样的错误
-
为什么需要 Fruits 课程?您的 EntryLog 有一个
@ElementList(name = "fruits"),即List<FruitItem> -
这只是一个猜测,尝试将
FruitItem设为EntryLog或Fruits的嵌套类。 -
原来需要为 ElementList 添加
inline=true。不知道如何解决它,但它工作。谢谢大家。
标签: java android xml retrofit2