Android kotlin SimpleXmlConverterFactory 在读取属性时崩溃
Android kotlin SimpleXmlConverterFactory crashes when reading attributes
我正在尝试用 Retrofit2 和 SimpleXmlConverterFactory 解析 XML,这一直有效,直到我尝试读取一个节点的属性,在那里,它崩溃了,没有日志。
我的 XML :
<HotelSearchRES>
<Destination id="1">Tunisie</Destination>
<City id="104">Hammamet</City>
<Currency>1</Currency>
<Language>1</Language>
<FromDate>01/12/2021</FromDate>
<ToDate>02/12/2021</ToDate>
</HotelSearchRES>
我的 类 :
@Root(name = "HotelSearchRES", strict = false)
class SearchResultsWrapper @JvmOverloads constructor() {
@Element(name = "Destination")
var destination: Destination? = null
@set:Element(name = "City")
@get:Element(name = "City")
var city: String? = null
@set: Element(name = "Currency")
@get: Element(name = "Currency")
var currency: Int? = null
@set: Element(name = "Language")
@get: Element(name = "Language")
var language: Int? = null
@set: Element(name = "FromDate")
@get: Element(name = "FromDate")
var fromDate: String? = null
@set: Element(name = "ToDate")
@get: Element(name = "ToDate")
var toDate: String? = null
}
class Destination(){
@Attribute(required = false)
val id: String? = null
@Text(required = false)
val destination: String? = null
}
问题接缝与属性读取有关,所以如果我删除第一行,一切正常:
@Element(name = "Destination")
var destination: Destination? = null
如有任何帮助,我们将不胜感激 ;)
在 Kotlin 中,字段仅用作 属性 的一部分以将其值保存在内存中。不能直接声明字段。但是,当 属性 需要一个支持字段时,Kotlin 会自动提供它。可以使用 field
标识符
在访问器中引用此支持字段
做类似的事情:
@field:Element(name = "Destination")
var destination: Destination? = null
顺便说一下,您不需要对属性执行 @get
、@set
,您可以使用 @field
请参阅 this or here Annotations 了解详细答案
好的,对于那些搞乱解析属性的人,这里是正确的 class 注释:
@Root(name = "HotelSearchRES", strict = false)
class HotelSearchRES @JvmOverloads constructor(){
@field:Element(name = "Destination") var destination: Destination? = null
@field:Element(name = "City") var city: City? = null
@field:Element(name = "Currency") var currency: Int = 0
@field:Element(name = "Language") var language: Int = 0
@field:Element(name = "FromDate") var fromDate: String? = null
@field:Element(name = "ToDate") var toDate: String? = null
}
@Root(name = "Destination", strict = false)
class Destination {
@field:Attribute(name = "id", required = false) var id: Int = 0
@field:Text var text: String? = null
}
@Root(name = "City", strict = false)
class City {
@field:Attribute(name = "id", required = false) var id: Int = 0
@field:Text var text: String? = null
}
感谢@MG1616 的技巧
我正在尝试用 Retrofit2 和 SimpleXmlConverterFactory 解析 XML,这一直有效,直到我尝试读取一个节点的属性,在那里,它崩溃了,没有日志。
我的 XML :
<HotelSearchRES>
<Destination id="1">Tunisie</Destination>
<City id="104">Hammamet</City>
<Currency>1</Currency>
<Language>1</Language>
<FromDate>01/12/2021</FromDate>
<ToDate>02/12/2021</ToDate>
</HotelSearchRES>
我的 类 :
@Root(name = "HotelSearchRES", strict = false)
class SearchResultsWrapper @JvmOverloads constructor() {
@Element(name = "Destination")
var destination: Destination? = null
@set:Element(name = "City")
@get:Element(name = "City")
var city: String? = null
@set: Element(name = "Currency")
@get: Element(name = "Currency")
var currency: Int? = null
@set: Element(name = "Language")
@get: Element(name = "Language")
var language: Int? = null
@set: Element(name = "FromDate")
@get: Element(name = "FromDate")
var fromDate: String? = null
@set: Element(name = "ToDate")
@get: Element(name = "ToDate")
var toDate: String? = null
}
class Destination(){
@Attribute(required = false)
val id: String? = null
@Text(required = false)
val destination: String? = null
}
问题接缝与属性读取有关,所以如果我删除第一行,一切正常:
@Element(name = "Destination")
var destination: Destination? = null
如有任何帮助,我们将不胜感激 ;)
在 Kotlin 中,字段仅用作 属性 的一部分以将其值保存在内存中。不能直接声明字段。但是,当 属性 需要一个支持字段时,Kotlin 会自动提供它。可以使用 field
标识符
做类似的事情:
@field:Element(name = "Destination")
var destination: Destination? = null
顺便说一下,您不需要对属性执行 @get
、@set
,您可以使用 @field
请参阅 this or here Annotations 了解详细答案
好的,对于那些搞乱解析属性的人,这里是正确的 class 注释:
@Root(name = "HotelSearchRES", strict = false)
class HotelSearchRES @JvmOverloads constructor(){
@field:Element(name = "Destination") var destination: Destination? = null
@field:Element(name = "City") var city: City? = null
@field:Element(name = "Currency") var currency: Int = 0
@field:Element(name = "Language") var language: Int = 0
@field:Element(name = "FromDate") var fromDate: String? = null
@field:Element(name = "ToDate") var toDate: String? = null
}
@Root(name = "Destination", strict = false)
class Destination {
@field:Attribute(name = "id", required = false) var id: Int = 0
@field:Text var text: String? = null
}
@Root(name = "City", strict = false)
class City {
@field:Attribute(name = "id", required = false) var id: Int = 0
@field:Text var text: String? = null
}
感谢@MG1616 的技巧