gpt4 book ai didi

android - 在Android上检索错误对象而不是HTTP 400

转载 作者:行者123 更新时间:2023-12-03 09:01:39 25 4
gpt4 key购买 nike

我在Android上使用apollo client以通过授权使用graphql服务。

当该认证 token 过期时,将显示错误:HTTP 400而不是实际的服务器错误:

{
"errors": [
{
"message": "Invalid token.",
...
}
]
}

从技术上讲,这应该执行 onSuccess,但应执行 hasErrors而不是 onFailure
override fun onFailure(e: ApolloException) {
//I receive the error here.
}

override fun onResponse(response: Response<T>) {
if (!response.hasErrors()) {
//Success
} else {
//I should receive the error here...
}
}

这和 customType有关吗?

最佳答案

实际上,Apollo与Retrofit非常相似,后者均使用 OkHttp 来处理响应。 Apollo处理HTTP响应的方式遵循OkHttp规则和行为。根据OkHttp DocsisSuccess函数确定成功回调:

Returns true if the code is in [200..300], which means the request was successfully received, understood, and accepted.



在Apollo中,这意味着只要状态代码不在200-300之间,它将 总是进入 onFailure回调(在您的情况下,您是由于 HTTP 400才得到它)。

使代码进入 onSuccess回调的唯一方法是在后端捕获异常,然后将 HTTP 200与错误消息一起发送,而不是 HTTP 400。我的团队做到了,而且行得通。

关于android - 在Android上检索错误对象而不是HTTP 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60731315/

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