gpt4 book ai didi

android - errorBody() 应为 BEGIN_ARRAY

转载 作者:行者123 更新时间:2023-11-29 16:38:31 27 4
gpt4 key购买 nike

我正在尝试将 errorBody 转换为我的 RegistrationResponse 但它总是抛出一个 Expected BEGIN_ARRAY but was BEGIN_OBJECT

注册响应

data class RegistrationResponse(
val status: Boolean,
val errors: List<RegistrationBody>
)

注册主体

data class RegistrationBody(
val lastname: String,
val firstname: String,
val middlename: String
)

我的转换器

if (it is HttpException) {
if (it.code() == 400) {
// Bad Request
val errorConverter: Converter<ResponseBody, RegistrationResponse>? = retro?.responseBodyConverter(RegistrationResponse::class.java, arrayOfNulls<Annotation>(0))
Log.i("TAG", "${errorConverter?.convert(it.response().errorBody())}")
}
}

我的预期输出

{                       
status: false,
errors: {
lastName: "The last name is required.",
firstName: "The first name is required.",
middleName: "The middle name is required.",
}
}

我是否遗漏了代码中的某些内容?

我也试试

val errorConverter: Converter<ResponseBody, List<RegistrationResponse>>? = retro?.responseBodyConverter(RegistrationResponse::class.java, arrayOfNulls<Annotation>(0))

错误仍然是 Expected BEGIN_ARRAY but was BEGIN_OBJECT

最佳答案

Expected BEGIN_ARRAY but was BEGIN_OBJECT

RegistrationBodyOBJECT 而不是 ARRAY

你应该使用

    data class RegistrationResponse(
val status: Boolean,
val errors: RegistrationBody
)

关于android - errorBody() 应为 BEGIN_ARRAY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51889091/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com