*解决方案在第一个答案中
我非常喜欢使用SimpleXML Converter
,所以模型和注释可能是错误的。
这是我收到的XML
:
<?xml version="1.0" encoding="UTF-8"?>
<cards-summary-response xmlns="http:/mywebpage.com/rest-api">
<card-summary>
<identifier>51641C1B23931</identifier>
<display-name>Debit Card</display-name>
<program-name>plastic_debit</program-name>
<status>Blocked</status>
</card-summary>
<card-summary>
<identifier>4E1BDFCC1D6</identifier>
<display-name>Virtual Debit</display-name>
<program-name>virtual_debit</program-name>
<status>Active</status>
</card-summary>
</cards-summary-response>
我的模特:
CardSummaryResponse.kt
@Root(strict = false, name = "cards-summary-response")
data class CardSummaryResponse constructor(
@ElementList(
required = false,
name = "card-summary",
entry = "card-summary",
inline = true,
empty = true
)
var cardsSummaryList: MutableList<CardSummary>? = null
)
CardSummary.kt
@Root(name = "card-summary")
data class CardSummary @JvmOverloads constructor(
@field:Element(name = "identifier", required = true)
var identifier: String = "",
@field:Element(name = "status", required = true)
var status: String = ""
)
API配置:
retrofit = Retrofit.Builder()
.addConverterFactory(SimpleXmlConverterFactory.createNonStrict(
Persister(AnnotationStrategy())
)
)
.baseUrl(CallsConfiguration.BASE_URL_WIRECARD)
.client(clientBuilder.build())
.build()
service = retrofit.create(RetrofitInterface::class.java)
和RetrofitInterface(此类在Java中):
@GET("webservices/wd-test.phtml")
Call<CardSummaryResponse> getCardsSummary();
我总是遇到相同的异常:
java.lang.Error:org.simpleframework.xml.core.ConstructorException: 参数“ card-summary”在类中不匹配 com.app.account.model.CardSummaryResponse
有什么建议吗?
答案 0 :(得分:2)
就我而言,我在数据类中使用了'val'
。花了3个小时后,我找到了解决方案:只需将'val
替换为'var'
,然后给default value.
以下是我的数据类,它们在解决方案之后运行良好:
sealed class Feeds {
@Root(name = "rss", strict = false)
data class RssFeed constructor(@field:Element(name = "channel") var channel: RssChannel? = null) :
Feeds()
@Root(name = "channel", strict = false)
data class RssChannel constructor(
@field:ElementList(
name = "item",
inline = true,
required = false
) var item: List<RssItem>? = null
) : Feeds()
@Root(name = "item", strict = false)
data class RssItem constructor(
@field:Element(name = "title") var title: String = "",
@field:Element(name = "description") var description: String = "",
@field:Element(name = "link") var link: String = ""
) : Feeds()
}
快乐编码!!!
答案 1 :(得分:1)
我设法使用以下模型使其工作:
CardSummaryResponse.tk
@Root(strict = false, name = "cards-summary-response")
data class CardSummaryResponse constructor(
@field:ElementList(
required = false,
name = "card-summary",
entry = "card-summary",
inline = true,
empty = true
)
var cardsSummaryList: MutableList<CardSummary>? = null
)
CardSummary.tk
@Root(name = "card-summary")
data class CardSummary constructor(
@field:Element(name = "identifier", required = true)
var identifier: String = "",
@field:Element(name = "status", required = true)
var status: String = ""
)