2017-08-17 177 views
1

我正在使用retrofitSimpleXMLConverterFactory參數不匹配; SimpleXML

,我總是得到一個

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

而且我不知道什麼可能是錯誤的。 xml非常簡單,我只需要success節點中的字符串。

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。

感謝

回答

3

所以最後,我設法自己解決問題。

問題在於ResponseInfo類。我改成

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

一切正常。

你需要有一個空的構造,所有屬性必須是可變的(var),你在的@Element -Annotation前追加field:@JvmOverloads結合默認值將爲您創建空構造函數以及所有其他構造函數變體。

1

如果你想避免使用deafult構造函數,你將不得不使用fieldparamuse-site target s。這將是這個樣子:

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

正如this comment說,它好像沒有兩者結合使用,網站目標的任何方式。