gpt4 book ai didi

android - 我如何让 Robospice 将来自 Retrofit 和 OKHttp 的 200 响应以外的任何其他内容视为错误

转载 作者:太空宇宙 更新时间:2023-11-03 10:20:48 25 4
gpt4 key购买 nike

我在带有 Retrofit 和 OKHttp 的 Android 上使用 Robospice。使用 Robospice RequestListener 将响应传递回 Activity ,一切都很好。问题是它只在连接超时或其他网络问题时返回失败。如果返回 401,则它被归类为成功,但响应为空,因为它无法将 JSON 解析到我们的 MobileAppSetup POJO 中。

目前我必须对响应进行空检查,但我不知道是服务器错误还是有效的 401 是什么原因。

public final class HTTPRequestListener implements RequestListener<MobileAppSetup> {

@Override
public void onRequestFailure(SpiceException spiceException) {
Log.d("", "failure:"+ spiceException);
loginProgress.hide();

}

@Override
public void onRequestSuccess(final MobileAppSetup response) {
Log.d("","success. Data: "+response);

if(response==null)
showDialog("Error logging in", "Please check your username and password and try again.", "");

loginProgress.hide();

postLoginProcess(response);

}
}

我需要将这些错误传递给 onRequestFailure 回调,以便我可以正确处理它。有没有一种方法可以指定 Robospice 应将其视为错误的错误代码。我认为它涉及添加某种自定义错误处理程序,但目前确实找不到解决方案。

最佳答案

这是由于 OKHTTP 客户端 中的错误 possible bug !

Problem is When the server answers with a 401 Unauthorized responses, the Callback object calls its failure() method..

When the server returns with the 401 status, the RetrofitError response object is null, and the networkError flag is set to true, making it impossible for the app to check error.getResponse().getStatus() I believe the error lies on the http client. When using OkClient I get this error: java.io.IOException: No authentication challenges found

我建议你从 square/okhttp 下载新的 okhttp jar 文件再次运行项目!或尝试使用任何其他客户端而不是 okhttp。

关于android - 我如何让 Robospice 将来自 Retrofit 和 OKHttp 的 200 响应以外的任何其他内容视为错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22633415/

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