Retrofit2 的使用与封装

Retrofit2 的使用与封装

响应基类

public class BaseResponse {
    public String code;
    public String message;
} 

请求数据对象基类

public class BaseRequest {
    public String productGuid = BuildConfig.PRODUCT_GUID;
    public int version = 1;
    public int clientType = 1;
}

BaseNetClient

public class BaseNetClient {
...
    private BaseNetClient() {
        Retrofit.Builder builder = new Retrofit.Builder();
        //定义网络请求域名
        builder.baseUrl(BuildConfig.HOST)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())// 支持RxJava2
                .addConverterFactory(GsonConverterFactory.create());

        //调试模式下打印日志
        if (BuildConfig.LOG_DEBUG) {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(Config.LOG_MODE);

            OkHttpClient client = new OkHttpClient.Builder()
                    .addInterceptor(interceptor)
                    .retryOnConnectionFailure(true)
                    .connectTimeout(5, TimeUnit.SECONDS)
                    .build();
            builder.client(client);
        }

        retrofit = builder.build();

    }

    //传入service接口
    public <T> T createService(Class<T> service) {
        return retrofit.create(service);
    }
}

BaseNetHelper

public abstract class BaseNetHelper<T> implements BaseINetHelper {

    protected T service;
    private Call call;

    protected BaseNetHelper(Class<T> service) {
        if (service != null && this.service == null) {
            this.service = BaseNetClient.getInstance()
                    .createService(service);
        }
    }

    protected <E extends BaseResponse> void initCall(Call<E> call, BaseCallBack<E> callBack) {
        initCall(false, call, callBack);
    }

    protected <E extends BaseResponse> void initCall(boolean cancelCallBefore, Call<E> call, BaseCallBack<E> callBack) {
        if (cancelCallBefore) cancelCall();
        call.enqueue(callBack);
        this.call = call;
    }

    @Override
    public void cancelCall() {
        if (call != null) {
            call.cancel();
            call = null;
        }
    }
}

BaseCallBack

public abstract class BaseCallBack<T extends BaseResponse> implements Callback<T> {

    @Override
    public void onResponse(Call<T> call, Response<T> response) {

        try {
            if (response.body() != null) {

                String code = response.body().code;
                // 与后端约定的返回code的处理
                if (code.startsWith("2")) {
                    onSuccess(code, response.body());
                } else if (code.startsWith("3")) {
                    onFailure(code, response.body().message);
                } else if (code.startsWith("4")) {
                    // 提交奔溃信息
                    CrashReport.postCatchedException(new Throwable(response.body().code + ":" + response.body().message));
                    onFailure(code, "请求异常 + 异常码: " + code);
                } else {
                    onFailure("999", "未知异常");
                }
            }else {
                onFailure(ERROR_DATABASE_ERROR, response.errorBody().toString());
            }
        }catch (Exception e){
            onFailure("999", "数据错误");
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        // 请求失败统一处理
    }

    //返回标志success为1的时候回调
    protected abstract void onSuccess(String code, T result);

    //返回标志success不为
    protected abstract void onFailure(String code, String message);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值