ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 如何让Robospice处理来自Retrofit和OKHttp的200响应以外的任何错误

2019-07-03 09:12:57  阅读:283  来源: 互联网

标签:android httprequest retrofit okhttp robospice


我使用Retrofit和OKHttp在Android上使用Robospice.所有工作都很好,使用Robospice RequestListener将响应传递回活动.问题是,如果连接超时或其他网络问题,它只会返回失败.如果返回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,httprequest,retrofit,okhttp,robospice
来源: https://codeday.me/bug/20190703/1365308.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有