I'm using retrofit for web requests and then moshi for JSON parsing,this is api
@POST("/verify_code/send_email")
suspend fun sendEmail(@Body sendEmailRequest: SendEmailRequest): BaseResponse<Unit>
the BaseResponse
@JsonClass(generateAdapter = true)
open class BaseResponse<T> {
@Json(name = "code")
var code: Int? = null
@Json(name = "message")
var message: String? = null
@Json(name = "data")
var data: T? = null
}
JSON String
{
"code": 200,
"message": "Some Message",
"data": null
}
and error log
2021-11-26 09:59:24.166 14288-14288/com.gow E/FlowKtxKt$next: java.lang.IllegalArgumentException: Unable to create converter for com.gow.base.BaseResponse<kotlin.Unit>
for method VerifyApi.sendEmail
I tried adding the following, but it didn't work
object UnitConverterFactory : Converter.Factory() {
override fun responseBodyConverter(
type: Type, annotations: Array<out Annotation>,
retrofit: Retrofit
): Converter<ResponseBody, *>? {
return if (type == Unit::class.java) UnitConverter else null
}
private object UnitConverter : Converter<ResponseBody, Unit> {
override fun convert(value: ResponseBody) {
value.close()
}
}
}
it`s the Moshi bug.
I solved my problem by using Any
instead of Unit
.
like this:
@POST("/verify_code/send_email")
suspend fun sendEmail(@Body sendEmailRequest: SendEmailRequest): BaseResponse<Any>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.