Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
889 views
in Technique[技术] by (71.8m points)

kotlin - Parameter does not have a match; SimpleXML

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...