gpt4 book ai didi

android - 即使模型正确,来自 API 的响应也不会映射到对象

转载 作者:行者123 更新时间:2023-12-04 08:00:42 25 4
gpt4 key购买 nike

我正在使用改造来发出这样的 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
)

关于android - 即使模型正确,来自 API 的响应也不会映射到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66487494/

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