Retrofit全攻略——进阶篇

最近事比较多,距离上次写文章已经过去了一个月了。上一篇文章Retrofit全攻略——基础篇 介绍了Retrofit的基础用法,这篇文章介绍点进阶的用法。

打印网络日志

在开发阶段,为了方便调试,我们需要查看网络日志。因为Retrofit2.0+底层是采用的OKHttp请求的。可以给OKHttp设置拦截器,用来打印日志。
首先可以在app/build.gradle中添加依赖,这是官方的日志拦截器。

compile 'com.squareup.okhttp3:logging-interceptor:3.3.0'

然后在代码中设置:

    public static Retrofit getRetrofit() {
        //如果mRetrofit为空  或者服务器地址改变 重新创建
        if (mRetrofit == null) {
            OkHttpClient httpClient;
            OkHttpClient.Builder builder=new OkHttpClient.Builder();
            
            //阶段分为开发和发布阶段,当前为开发阶段设置拦截器
            if (BuildConfig.DEBUG) {
                HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
                //设置拦截器级别
                logging.setLevel(HttpLoggingInterceptor.Level.BODY);
                builder.addInterceptor(logging);
            }
            httpClient=builder.build();
            //构建Retrofit
            mRetrofit = new Retrofit.Builder()
                    //配置服务器路径
                    .baseUrl(mServerUrl)
                    //返回的数据通过Gson解析
                    .addConverterFactory(GsonConverterFactory.create())
                    //配置回调库,采用RxJava
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    //设置OKHttp模板
                    .client(httpClient)
                    .build();
        }
        return mRetrofit;
    }

当处于开发阶段的时候,设置监听日志的拦截器。拦截有4个级别,分别是

  1. BODY
  2. HEADERS
  3. BASIC
  4. NONE

其中BODY输出的日志是最全的。

添加相同的请求参数

为了更好的管理迭代版本,一般每次发起请求的时候都传输当前程序的版本号到服务器。
有些项目我们每次还会传用户id,token令牌等相同的参数。
如果在每个请求的接口都添加这些参数太繁琐。Retrofit可以通过拦截器添加相同的请求参数,无需再每个接口添加了。

步骤一,自己拦截器

public class CommonInterceptor implements Interceptor {
    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request oldRequest = chain.request();

        // 添加新的参数
        HttpUrl.Builder authorizedUrlBuilder = oldRequest.url()
                .newBuilder()
                .scheme(oldRequest.url().scheme())
                .host(oldRequest.url().host())
                .addQueryParameter("device_type", "1")
                .addQueryParameter("version", BuildConfig.VERSION_NAME)
                .addQueryParameter("token", PreUtils.getString(R.string.token))
                .addQueryParameter("userid", PreUtils.getString(R.string.user_id));

        // 新的请求
        Request newRequest = oldRequest.newBuilder()
                .method(oldRequest.method(), oldRequest.body())
                .url(authorizedUrlBuilder.build())
                .build();

        return chain.proceed(newRequest);
    }
}

实现原理就是拦截之前的请求,添加完参数,再传递新的请求。这个位置我添加了四个公共的参数。
然后再Retrofit初始化的时候配置。

        if (mRetrofit == null) {
            OkHttpClient httpClient;
            OkHttpClient.Builder builder=new OkHttpClient.Builder();
            //添加公共参数
            builder.addInterceptor(new CommonInterceptor());
            httpClient=builder.build();
            //构建Retrofit
            mRetrofit = new Retrofit.Builder()
                    //....
                    .client(httpClient)
                    .build();
        }

处理约定错误

除了常见的404,500等异常,网络请求中我们往往还会约定些异常,比如token失效,账号异常等等。

以token失效为例,每次请求我们都需要验证是否失效,如果在每个接口都处理一遍错误就有点太繁琐了。

我们可以统一处理下错误。

步骤一,Retrofit初始化时添加自定义转化器

mRetrofit = new Retrofit.Builder()
        //配置服务器路径
      baseUrl(mServerUrl)
      //配置回调库,采用RxJava
     .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
     //配置转化库,默认是Gson,这里修改了。
     .addConverterFactory(ResponseConverterFactory.create())
     .client(httpClient)
     .build();

步骤二 创建ResponseConverterFactory

步骤一 ResponseConverterFactory这个类是需要我们自己创建的。

public class ResponseConverterFactory extends Converter.Factory {
    /**
     * Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and
     * decoding from JSON (when no charset is specified by a header) will use UTF-8.
     */
    public static ResponseConverterFactory create() {
        return create(new Gson());
    }

    /**
     * Create an instance using {@code gson} for conversion. Encoding to JSON and
     * decoding from JSON (when no charset is specified by a header) will use UTF-8.
     */
    public static ResponseConverterFactory create(Gson gson) {
        return new ResponseConverterFactory(gson);
    }

    private final Gson gson;

    private ResponseConverterFactory(Gson gson) {
        if (gson == null) throw new NullPointerException("gson == null");
        this.gson = gson;
    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
                                                            Retrofit retrofit) {
      //  TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new GsonResponseBodyConverter<>(gson, type);
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type,
                                                          Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new GsonRequestBodyConverter<>(gson, adapter);
    }
}

这里面我们自定义了请求和响应时解析JSON的转换器——GsonRequestBodyConverterGsonResponseBodyConverter

其中GsonRequestBodyConverter 负责处理请求时传递JSON对象的格式,不需要额外处理任何事,直接使用默认的GSON解析。代码我直接贴出来:

final class GsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
    private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
    private static final Charset UTF_8 = Charset.forName("UTF-8");

    private final Gson gson;
    private final TypeAdapter<T> adapter;

    GsonRequestBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override public RequestBody convert(T value) throws IOException {
        Buffer buffer = new Buffer();
        Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
        JsonWriter jsonWriter = gson.newJsonWriter(writer);
        adapter.write(jsonWriter, value);
        jsonWriter.close();
        return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
    }
}

GsonResponseBodyConverter负责把响应的数据转换成JSON格式,这个我们需要处理一下。


public class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Gson gson;
    private final Type type;

    GsonResponseBodyConverter(Gson gson, Type type) {
        this.gson = gson;
        this.type = type;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        String response = value.string();
        try {
            Log.i("YLlibrary", "response>>> "+response);
            //ResultResponse 只解析result字段
            BaseInfo baseInfo = gson.fromJson(response, BaseInfo.class);
            if (baseInfo.getHeader().getCode().equals("1")) {
                //正确
                return gson.fromJson(response, type);
            } else {
                //ErrResponse 将msg解析为异常消息文本 错误码可以自己指定.
                throw new ResultException(-1024, baseInfo,response);
            }
        } finally {
        }
    }

}

这种情况只是应用于后台接口数据统一的情况。比如我们项目的格式是这样的

      {
          header : {"message":"token失效","code":"99"}
          data : {}
      }

当code值是1的时候,表示正确,其它数字表示错误。只有正确的时候data才会有内容。

这里我用BaseInfo解析这个JSON:

public class BaseInfo {

    /**
     * header : {"message":"用户名或密码错误","code":"0"}
     * data : {}
     */

    private HeaderBean header;

    public HeaderBean getHeader() {
        return header;
    }

    public void setHeader(HeaderBean header) {
        this.header = header;
    }


    public static class HeaderBean {
        /**
         * message : 用户名或密码错误
         * code : 0
         */

        private String message;
        private String code;

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }
    }
}

服务器返回的数据实体对象全部继承BaseInfo 只是data内容不一样。

ResultException这个类用于捕获服务器约定的错误类型

/**
 * 这个类用于捕获服务器约定的错误类型
 */
public class ResultException extends RuntimeException {

    private int errCode = 0;
    private BaseInfo info;
    private String response;
    public ResultException(int errCode, BaseInfo info,String response) {
        super(info.getHeader().getMessage());
        this.info=info;
        this.errCode = errCode;
        this.response=response;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }

    public int getErrCode() {
        return errCode;
    }

    public BaseInfo getBaseInfo(){
        return info;
    }
}

最后定义Retrofit处理异常的代码

public abstract class AbsAPICallback<T> extends Subscriber<T> {

    //对应HTTP的状态码
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;
    //出错提示
    private final String networkMsg;
    private final String parseMsg;
    private final String unknownMsg;

    protected AbsAPICallback(String networkMsg, String parseMsg, String unknownMsg) {
        this.networkMsg = networkMsg;
        this.parseMsg = parseMsg;
        this.unknownMsg = unknownMsg;
    }
    public  AbsAPICallback(){
        networkMsg="net error(联网失败)";
        parseMsg="json parser error(JSON解析失败)";
        unknownMsg="unknown error(未知错误)";
    }
    ProgressBar progressBar;
    public  AbsAPICallback(ProgressBar progressBar){
        this();
        this.progressBar=progressBar;
    }
    @Override
    public void onError(Throwable e) {
        Throwable throwable = e;
        //获取最根源的异常
        while(throwable.getCause() != null){
            e = throwable;
            throwable = throwable.getCause();
        }

        ApiException ex;
        if (e instanceof HttpException){             //HTTP错误
            HttpException httpException = (HttpException) e;
            ex = new ApiException(e, httpException.code());
            switch(httpException.code()){
                case UNAUTHORIZED:
                case FORBIDDEN:
                  //  onPermissionError(ex);          //权限错误,需要实现
                   // break;
                case NOT_FOUND:
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                case INTERNAL_SERVER_ERROR:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                default:
                    ex.setDisplayMessage(networkMsg);  //均视为网络错误
                    onNetError(ex);
                    break;
            }
        } else if (e instanceof ResultException){    //服务器返回的错误
            ResultException resultException = (ResultException) e;
            onResultError(resultException);
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException){
            ex = new ApiException(e, ApiException.PARSE_ERROR);
            ex.setDisplayMessage(parseMsg);            //均视为解析错误
            onNetError(ex);
        } else {
            ex = new ApiException(e, ApiException.UNKNOWN);
            ex.setDisplayMessage(unknownMsg);          //未知错误
            onNetError(ex);
        }
    }
    static long time;
    protected  void onNetError(ApiException e){
        long currentTime=System.currentTimeMillis();
        if(currentTime-time>3000){  //防止连续反馈
            time=currentTime;
            UIUtils.showToast("网络加载失败");
        }
        e.printStackTrace();
        onApiError(e);
    }
    /**
     * 错误回调
     */
    protected  void onApiError(ApiException ex){
        Log.i("YLLibrary","onApiError");
        if(progressBar!=null)
            UIUtils.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progressBar.setVisibility(View.GONE);
                    progressBar=null;
                }
            });
    }

//    /**
//     * 权限错误,需要实现重新登录操作
//     */
//    protected void  onPermissionError(ApiException ex){
//        ex.printStackTrace();
//    }

    /**
     * 服务器返回的错误
     */
    protected  synchronized  void onResultError(ResultException ex){
//        if(ex.getErrCode()== XApplication.API_ERROR){
//            UIUtils.getContext().onApiError(); //可以用来处理Token失效
//            return ;
//        }
        if(ConstantValue.TOKEN_ERROR.equals(ex.getBaseInfo().getHeader().getCode())
                &&!TextUtils.isEmpty(PreUtils.getString(R.string.token))){ //验证token是否为空是为了防止连续两次请求
            PreUtils.putString(R.string.user_id,null);
            PreUtils.putString(R.string.token,null);
            PreUtils.putString(R.string.orgDistrict,null);
            if(BaseActivity.runActivity!=null){
                Intent intent = new Intent(UIUtils.getContext(), LoginActivity.class);
                if(BaseActivity.runActivity instanceof MainActivity){
                    MainActivity activity= (MainActivity) BaseActivity.runActivity;
                    int tabIndex=activity.getCurrentTab();
                    //activity.switchCurrentTab(0);
                    activity.startActivityForResult(intent,tabIndex+10);
                }else {
                    BaseActivity.runActivity.startActivity(intent);
                }
            }
        }


        Log.i("YLLibrary","resultError");
        if(ex.getBaseInfo()!=null&&!TextUtils.isEmpty(ex.getBaseInfo().getHeader().getMessage()))
            UIUtils.showToast(ex.getBaseInfo().getHeader().getMessage());

        ApiException apiException = new ApiException(ex, ex.getErrCode());
        onApiError(apiException);
    }

    @Override
    public void onCompleted() {
        Log.i("YLLibrary","onCompleted");
        if(progressBar!=null)
            UIUtils.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progressBar.setVisibility(View.GONE);
                    progressBar=null;
                }
            });
    }

}

实际接口请求的代码,使用自定义异常回调的类——AbsAPICallback就可以统一处理异常:

        ApiRequestManager.createApi().problemDetail(dataBean.getId())
                .compose(ApiRequestManager.<QuestionDetailInfo>applySchedulers())
                .subscribe(new AbsAPICallback<QuestionDetailInfo>() {
                    @Override
                    public void onNext(QuestionDetailInfo baseInfo) {
                        fillData(baseInfo);
                    }
                });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,566评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,274评论 25 707
  • 又是一年中秋佳节,祝各位中秋节快乐。 今天我们来聊聊这个最近很火的网络请求库retrofit,在此基础上会延伸出一...
    涅槃1992阅读 7,768评论 13 133
  • 昨天过来一直打点滴,到现在还没有打完。朋友回去了,老公还没有回来,女儿她干妈带着,听干妈说,女儿昨天要把妈妈找回来...
    嘉俐阅读 213评论 0 0
  • ——阅读感觉之六 熊老师您好,我是大二的学生,每天心里都有着许多要做的事,但是我却从来没有完成过,我每天都活在自责...
    熊玲心理咨询阅读 736评论 0 1