我正在尝试将errorBody
转换为我的RegistrationResponse
但它总是抛出Expected BEGIN_ARRAY but was BEGIN_OBJECT
RegistrationResponse
data class RegistrationResponse(
val status: Boolean,
val errors: List<RegistrationBody>
)
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
预期BEGIN_ARRAY,但BEGIN_OBJECT
RegistrationBody
是对象而不是数组。
你应该使用
data class RegistrationResponse(
val status: Boolean,
val errors: RegistrationBody
)