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

标签 android httprequest robospice retrofit okhttp

我在带有 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/

相关文章:

c# - 如何从 http 图像创建 System.Drawing.Image

javascript - 如何检索 HTTP 响应错误的详细信息

java - RoboSpice 和 ORMLite - 访问数据

java - 如何将泛型类型的类作为参数传递?

android - 我得到 'android.content.res.Resources android.content.Context.getResources()'

java - 从其他 Activity/布局获取路径的问题

java - Spring HttpRequest 获取父 URL

android - Robospice - 我们应该以什么顺序使用 execute()、addListenerIfPending()、getFromCache()

java - Android 中的 apache 的 HttpClient?

android - 在edittext中输入值后TextInputLayout错误