android-Moshi将值反序列化为null
作者:互联网
我最近从Gson切换到Moshi,并且在解析某些Json时遇到了麻烦.
{
"access_token": "-LNe2LQ7DQH5Y2zs_W5iUumKuaUE",
"token_type": "bearer",
"device_id": "461f-837e-af5050c92fe9",
"expires_in": 3600,
"scope": "*"
}
这是模型类:
data class AuthToken(
@Json(name = "access_token") val accessToken: String,
@Json(name = "token_type") val tokenType: String,
@Json(name = "device_id") val deviceId: String,
@Json(name = "expires_in") val expiresIn: Int,
@Json(name = "scope") val scope: String
)
每当我在改造客户端中使用Moshi时,都会收到以下错误消息:
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull
我已经将该字段设为可空,但是始终将其反序列化为null.我已经检查了改造响应,并且使用Gson或Moshi时(显然)是相同的.我究竟做错了什么?
解决方法:
出于某种原因,当我明确告诉AuthToken类生成适配器时,我没有收到任何空值.
@JsonClass(generateAdapter = true)
data class AuthToken(
@Json(name = "access_token") val accessToken: String,
@Json(name = "token_type") val tokenType: String,
@Json(name = "device_id") val deviceId: String,
@Json(name = "expires_in") val expiresIn: Int,
@Json(name = "scope") val scope: String
)
标签:moshi,retrofit2,oauth-2-0,android 来源: https://codeday.me/bug/20191108/2008086.html