美文网首页
Retrofit2 的使用与封装

Retrofit2 的使用与封装

作者: 代码在码我 | 来源:发表于2019-01-04 17:03 被阅读3次

    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);
    }
    

    相关文章

      网友评论

          本文标题:Retrofit2 的使用与封装

          本文链接:https://www.haomeiwen.com/subject/dydkrqtx.html