我正在使用retrofit来发出这样的GET请求:
@GET("auth/facebook")
fun facebookSignUp(
@Query("access_token") access_token: String
): Observable<FacebookReturn>
然后我通过调用这个函数发出请求:
fun facebookSignUp(access_token: String)
:MutableLiveData<FacebookReturn>{
val items = MutableLiveData<FacebookReturn>()
disposable = apiServe.facebookSignUp(
access_token
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ result ->
items.postValue(result)
Log.d("Facebook API", result.toString())
},
{ error ->
Log.d("API ERROR", "API Fetch Error: ${error.message} ")
}
)
return items
}
API工作良好,返回的数据如下所示:
{
"facebook": {
"statusCode": 200,
"user": {
"email": "Disguised",
"firstName": "Disguised",
"lastName": "Disguised",
"token": "Disguised.eyJ1c2VybmFtZSI6bnVsbCwiZW1haWwiOiJwYXVsaXZhbl8zMDAwQHlhaG9vLmNvbSIsImlhdCI6MTYxNDg2NTk1OSwiZXhwIjoxNjE0ODY5NTU5fQ.q0Vn9_4d_-3_WcFfj9s7ZMD3I_0s6XWIEKvXCJuSa_w"
}
}
}
然后,我试图使用以下模型和GSON转换器库将此数据转换为POJO进行改造:
data class FacebookReturn(
val facebookUser: FacebookUser
)
data class FacebookUser(
val statusCode: Int,
val facebookUserDetails: FacebookUserDetails
)
data class FacebookUserDetails(
val email: String,
val firstName: String,
val lastName: String,
val token: String
)
然而,当我检查日志(标签:Facebook API)来查看对象时,它显示内容为空,即使API实际上返回了数据。下面是由HttpInterceptor创建的日志。我也看过类似的问题,但我不认为这是一个编码或映射问题,也许是,我不知道。
D/OkHttp: --> GET http://Disguised/auth/facebook?access_token=EAANAkZB2CDA4BAIhz74uPKfSZBO2qW52dcK8Qa3AzZASWCQ2Yu055yaUZATy71y92IGOiWpFwBsElRr8st4yhCRgbX2uIVauHMKA3TNyZBTsLcbkKbmMXFmnG9bP1ZBy3CNjJIqTRrqYbt76RhkIh9ZCg1A43kkfrzg6JBLZB9OoJRf5pEYbw8raJDl9nQD5sH5YpmzgD4J2Br4ZB4mHgMdHoFPXdRPdnxDORKcZCPUkkujQZDZD
--> END GET
I/rventures.tree: Background concurrent copying GC freed 28459(2397KB) AllocSpace objects, 24(1248KB) LOS objects, 49% free, 5115KB/10230KB, paused 1.052ms total 540.715ms
D/OkHttp: <-- 200 OK http://Disguised/auth/facebook?access_token=EAANAkZB2CDA4BAIhz74uPKfSZBO2qW52dcK8Qa3AzZASWCQ2Yu055yaUZATy71y92IGOiWpFwBsElRr8st4yhCRgbX2uIVauHMKA3TNyZBTsLcbkKbmMXFmnG9bP1ZBy3CNjJIqTRrqYbt76RhkIh9ZCg1A43kkfrzg6JBLZB9OoJRf5pEYbw8raJDl9nQD5sH5YpmzgD4J2Br4ZB4mHgMdHoFPXdRPdnxDORKcZCPUkkujQZDZD (651ms)
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 317
ETag: W/"13d-soe3iGc7MnKumoczrQKKLORbOpw"
Date: Fri, 05 Mar 2021 05:15:05 GMT
Connection: keep-alive
D/OkHttp: {"facebook":{"statusCode":200,"user":{"email":"disguised@yahoo.com","firstName":"Disguised","lastName":"Disguised","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6bnVsbCwiZW1haWwiOiJwYXVsaXZhbl8zMDAwQHlhaG9vLmNvbSIsImlhdCI6MTYxNDkyMTMwNSwiZXhwIjoxNjE0OTI0OTA1fQ.6ml4yIu8e7KdrRyIcrAaDDZ8yZqi298utm49FKKccN4"}}}
<-- END HTTP (317-byte body)
D/Facebook API: FacebookUserDetails(email=null, firstName=null, lastName=null, token=null)
D/Facebook Return: FacebookUserDetails(email=null, firstName=null, lastName=null, token=null)
这里的问题是什么?
类中字段的名称必须与JSON字段完全匹配,否则您将明确告诉GSON与@SerializedName("someName")
使用什么名称。
您的一些字段与JSON不匹配,这可能是您没有将任何数据放入其中的原因。JSON返回给"facebook": {}
作为外部对象,所以在FacebookReturn
中,你应该用这个序列化的名字标记val facebookUser
(或者重命名为"facebook")。
data class FacebookReturn(
@SerializedName("facebook")
val facebookUser: FacebookUser
)
在FacebookUser
中:
data class FacebookUser(
val statusCode: Int,
@SerializedName("user")
val facebookUserDetails: FacebookUserDetails
)