问题描述
我无法从200201等响应中获取成功响应状态代码.因为我们可以轻松地从RetrofitError
类(如error.isNetworkError()
和error.getResponse().getStatus()
)中获取错误代码.是否有任何解决方法来获取状态码?
I am not able to get success response status code from response like 200,201.. etc. As we can easily get error codes from RetrofitError
class like error.isNetworkError()
and error.getResponse().getStatus()
. Is there any workaround for getting status codes?
推荐答案
根据Retrofit 2.0.2,现在的呼叫是
As per Retrofit 2.0.2, the call is now
@Override
public void onResponse(Call<YourModel> call, Response<YourModel> response) {
if (response.code() == 200) {
// Do awesome stuff
} else {
// Handle other response codes
}
}
希望它对某人有帮助:-)
Hope it helps someone :-)
许多应用程序也可能会受益于仅检查一个子句中的成功(响应代码200-300),然后处理其他子句中的错误,如201(已创建)和202(已接受) )在大多数情况下可能会导致与200相同的应用逻辑.
Many apps could also benefit from just checking for success (response code 200-300) in one clause, and then handling errors in other clauses, as 201 (Created) and 202 (Accepted) would probably lead to the same app logic as 200 in most cases.
@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
}
}
这篇关于如何获得翻新成功响应状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!