如何获取 Retrofit 成功响应状态代码

2023-12-07

我无法从 200,201.. 等响应中获取成功响应状态代码,因为我们可以轻松地从RetrofitError类如error.isNetworkError() and error.getResponse().getStatus()。有没有获取状态代码的解决方法?


根据 Retrofit 2.0.2,现在调用

@Override
public void onResponse(Call<YourModel> call, Response<YourModel> response) {
    if (response.code() == 200) {
       // Do awesome stuff
    } else {
       // Handle other response codes
    }
}

希望它对某人有帮助:-)

EDIT:许多应用程序还可以受益于仅检查一个子句中的成功(响应代码 200-300),然后处理其他子句中的错误,因为 201(已创建)和 202(已接受)可能会导致与 200 中的 200 相同的应用程序逻辑。在大多数情况下。

@Override
public void onResponse(Call<YourModel> call, Response<YourModel> response) {
    if (response.isSuccessful()) {
       // Do awesome stuff
    } else if (response.code() == 401) {
       // Handle unauthorized
    } else {
       // Handle other responses
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取 Retrofit 成功响应状态代码 的相关文章

随机推荐