改造 - 不同的反应 [英] Retrofit - Different Response

查看:35
本文介绍了改造 - 不同的反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Retrofit 在 Android 中使用 API.成功响应看起来与错误/失败响应不同.因此,我如何才能实现这一目标?

I am consuming an API in Android using Retrofit. The success response looks different to the error/failure response. How can I therefore achieve this?

我目前对此有所影响:

Call<AuthenticateUserResponse> authenticateUser(String id);

推荐答案

您可以使用基本 Response 扩展您的 Responses 并检查是否存在错误或成功.下面是一个基本响应 bean 示例:

You can extend your Responses with a base Response and check if there's an error or it's succeed. Here's an example base response bean below:

public class BaseResponse {
    @SerializedName("ResponseCode")
    private int code;
    @SerializedName("ResponseMessage")
    private String message;
    @SerializedName("ResponseText")
    private String text;
}

我的 api 在每个响应者中返回 ResponseCodeResponseMessageResponseText,我从 BaseResponse bean 扩展我的响应并检查是否有错误.

My api returns ResponseCode, ResponseMessage and ResponseText in every responser and i extend my responses from BaseResponse bean and check if there's an error.

您可以修改您的响应作为 api 的返回方案.

You can modify your responses as your api's return schemes.

这是您对 Api 的回应:

Here's your Response for your Api:

public class Error {
        @SerializedName("ResponseCode")
        private int code;
        @SerializedName("ResponseMessage")
        private String message;
        @SerializedName("ResponseText")
        private String text;
    }

public class YourWrapperResponse {
        @SerializedName("Error")
        private Error error;
        @SerializedName("AuthenticateUserResponse")
        private AuthenticateUserResponse authenticateUserResponse;
    }

你的电话会是这样的:

Call<YourWrapperResponse> authenticateUser(String id);

上面我给您的示例是处理业务错误的示例,您在每次成功响应中都会遇到这些错误.成功意味着 Http 状态 200.此外,您不需要在每个响应中都返回此 Error object.如果有错误,您可以在回复中返回.

The example above i gave you is an example of handle business errors which you get in your every successful response. Successful means Http Status 200. Also you do not need to return this Error object in your every response. If there's an error you can return in your response.

在 Retrofit 2.0+ 中,您需要检查您的请求是否成功.下面是一个例子:

In Retrofit 2.0+ you need to check if your request is succeed. Here's an example below about it:

    Call<User> auth = YourApiProvider.getInstance().getServices().auth(userName, passowrd, grantType);
            auth.enqueue(new Callback<User>() {
                @Override
                public void onResponse(Call<User> call, Response<User> response) {
                    if (response.isSuccessful()) {
                        // Here you get a 200 from your server.
                        }
                    } else {
                        // Here you get an authentication error.
                        // You can get error with response.code();
                        // You can get your error with response.errorBody(); 
                        // or you can get raw response with response.raw()
                    }
                }

                @Override
                public void onFailure(Call<User> call, Throwable t) {
                    // Here you get error such as TimeOut etc.
                }
            });

希望对你有帮助.祝你好运!

I hope this'll help you. Good Luck!

您还可以使用泛型来处理基本 API 响应.这是我关于处理通用 API 响应的另一个答案.

You can also use generics to handle base api responses. Here's my another answer about handling generic api responses.

这篇关于改造 - 不同的反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆