Parameter does not have a match; SimpleXML

夙愿已清 提交于 2019-12-18 04:52:59

问题


I am using retrofit with the SimpleXMLConverterFactory.

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)

Edit 1: I tested the Api call and it returns the given xml.

Thanks


回答1:


So finally, I managed myself to solve the problem.

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 = ""
)

all worked fine.

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.




回答2:


If you want to avoid having a deafult constructor, you would have to use both field and param use-site targets. It would look something like this:

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

As stated in this comment it seems like there isn't any way of combining the two use-site targets.



来源:https://stackoverflow.com/questions/45741617/parameter-does-not-have-a-match-simplexml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!