我用的是retrofitSimpleXMLConverterFactory.

And I always get an

ConstructorException: Parameter 'success' does not have a match in class ResponseInfo

And I have no idea what could be wrong. The xml is very simple and i only want the string from the success node.

XML:

<?xml version="1.0" encoding="UTF-8"?>
<response>
    <success>LoremIpsum</success>
</response>

ResponseInfo:

@Root(strict = false, name = "response")
data class ResponseInfo(@Element(required = false, name = "success) var success: String)

编辑1:我测试了Api调用,它返回给定的xml.

Thanks

推荐答案

So finally, I managed to solve the problem myself.

The problem was the ResponseInfo class. After I changed it to

@Root(strict = false, name="response")
data class ResponseInfo @JvmOverloads constructor(
  @field:element(name = "success") var success: String = ""
)

一切都很好.

You need to have an empty constructor, all properties must be mutable (var) and you have to append field: in front of the @Element-Annotation. @JvmOverloads combined with default values will create the empty constructor for you as well as all other constructor variations.

Kotlin相关问答推荐

等待下一个值时暂停Kotlin Coroutine

用Quarkus和Reactor重写异步过滤器中的数据流

通过快捷键手动砍掉功能参数等

使用 StateFlow 时如何移除监听器?

如何使用成员引用在 Kotlin 中创建属性的分层路径

高效匹配两个 Flux

如何在 Hibernate Panache 中进行部分搜索

Kotlin 中列表或数组的乘积

Kotlin 的 isNullOrBlank() 函数是否可以导入 xml 以用于数据绑定

Kotlin 类的调用方法

Kotlin如何分派invoke操作符?

Kotlin协程处理错误和实现

禁用 IntelliJ kotlin * 导入?

ObserveForver是否了解生命周期?

这是 Kotlin 中的错误还是我遗漏了什么?

Dagger +Kotlin 不注入

在android java类中使用Kotlin扩展

旋转 kotlin 数组

Dagger 2 androidx fragment不兼容类型

如何判断数据是否插入到房间数据库中