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?
我无法从200,201等响应中获取成功响应状态代码。因为我们可以轻松地从RetrofitError类获取错误代码,如error.isNetworkError()和error.getResponse()。getStatus()。获取状态代码有什么解决方法吗?
3 个解决方案
#1
55
As per Retrofit 2.0.2, the call is now
根据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
}
}
Hope it helps someone :-)
希望它可以帮助某人:-)
EDIT: 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.
编辑:许多应用程序也可以从一个子句中检查成功(响应代码200-300),然后处理其他子句中的错误中受益,因为201(创建)和202(已接受)可能会导致相同的应用程序逻辑在大多数情况下为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
}
}
#2
15
you can get status code in success just like you do it in failure()
您可以像失败一样获得状态代码()
@Override
public void success(Object object, Response response) {
response.getStatus()//returns status code integer
}
since you have Response
object in success callback as response.getStatus()
因为你有成功回调的Response对象作为response.getStatus()
EDIT
I assume you are using okhttp with retrofit. ok http has a powerful tool called Interceptor
我假设您正在使用okhttp进行改造。 ok http有一个叫做Interceptor的强大工具
you can catch the response before retrofits Callback and get statuscode from response like this:
您可以在改装之前捕获响应回调并从响应中获取状态码,如下所示:
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor(){
@Override
public Response intercept(Chain chain) throws IOException{
Request request = chain.request();
Response response = chain.proceed(request);
response.code()//status code
return response;
});
//then add it to you Restclient like this:
restAdapter = new RestAdapter.Builder()
.setEndpoint(URL_SERVER_ROOT)
.setClient(new OkClient(client)) //plus your configurations
.build();
to learn more about interceptors visit here.
要了解更多有关拦截器的信息,请访问此处。
#3
5
i achieved it by following codes:
我通过以下代码实现了它:
public abstract class BaseCallBack<T> {
public abstract void onSuccess(Response<T> response);
public abstract void onFailure(Response<T> response);
}
public abstract class SuccessCallback<T> extends BaseCallBack<T> implements Callback<T>{
@Override
public void onResponse(Call<T> call, Response<T> response) {
if(response.code()>= 400 && response.code() < 599){
onFailure(response);
}
else {
onSuccess(response);
}
}
@Override
public void onFailure(Call<T> call, Throwable t){
}
@Override
public void onSuccess(Response<T> response) {
}
@Override
public void onFailure(Response<T> response) {
}
}
When the rest api returns 400 etc then the onFailure method will be call by default. If you wanna do something onsuccess:(when returns 200 etc)
当其余的api返回400等时,默认情况下将调用onFailure方法。如果你想成功的话:(当返回200等)
ServiceConnector.api.getHomePage().enqueue(new SuccessCallback<Void>() {
@Override
public void onSuccess(Response<Void> response) {
super.onSuccess(response);
}
});
#1
55
As per Retrofit 2.0.2, the call is now
根据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
}
}
Hope it helps someone :-)
希望它可以帮助某人:-)
EDIT: 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.
编辑:许多应用程序也可以从一个子句中检查成功(响应代码200-300),然后处理其他子句中的错误中受益,因为201(创建)和202(已接受)可能会导致相同的应用程序逻辑在大多数情况下为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
}
}
#2
15
you can get status code in success just like you do it in failure()
您可以像失败一样获得状态代码()
@Override
public void success(Object object, Response response) {
response.getStatus()//returns status code integer
}
since you have Response
object in success callback as response.getStatus()
因为你有成功回调的Response对象作为response.getStatus()
EDIT
I assume you are using okhttp with retrofit. ok http has a powerful tool called Interceptor
我假设您正在使用okhttp进行改造。 ok http有一个叫做Interceptor的强大工具
you can catch the response before retrofits Callback and get statuscode from response like this:
您可以在改装之前捕获响应回调并从响应中获取状态码,如下所示:
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor(){
@Override
public Response intercept(Chain chain) throws IOException{
Request request = chain.request();
Response response = chain.proceed(request);
response.code()//status code
return response;
});
//then add it to you Restclient like this:
restAdapter = new RestAdapter.Builder()
.setEndpoint(URL_SERVER_ROOT)
.setClient(new OkClient(client)) //plus your configurations
.build();
to learn more about interceptors visit here.
要了解更多有关拦截器的信息,请访问此处。
#3
5
i achieved it by following codes:
我通过以下代码实现了它:
public abstract class BaseCallBack<T> {
public abstract void onSuccess(Response<T> response);
public abstract void onFailure(Response<T> response);
}
public abstract class SuccessCallback<T> extends BaseCallBack<T> implements Callback<T>{
@Override
public void onResponse(Call<T> call, Response<T> response) {
if(response.code()>= 400 && response.code() < 599){
onFailure(response);
}
else {
onSuccess(response);
}
}
@Override
public void onFailure(Call<T> call, Throwable t){
}
@Override
public void onSuccess(Response<T> response) {
}
@Override
public void onFailure(Response<T> response) {
}
}
When the rest api returns 400 etc then the onFailure method will be call by default. If you wanna do something onsuccess:(when returns 200 etc)
当其余的api返回400等时,默认情况下将调用onFailure方法。如果你想成功的话:(当返回200等)
ServiceConnector.api.getHomePage().enqueue(new SuccessCallback<Void>() {
@Override
public void onSuccess(Response<Void> response) {
super.onSuccess(response);
}
});