Android之Retrofit基本用法篇

1.Retrofit基本用法

1.初始化

        webInterface = new Retrofit.Builder()
                .baseUrl(hostname)
                .client(client)//这个client是OkHttpClient,以后和Okhttp的基本用法和流程分析中细说
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(new Gson()))
                .build()
                .create(WebInterface.class);

2.定义网络接口

public interface WebInterface {

    @POST("/***/index")
    Observable getHomeIndex(@Body BaseRequest request);//首页

    @FormUrlEncoded
    @POST("/***/freecall")
    Observable freeCall(@Field("phone") String phone);//免费电话

}

3.在Activity中调用

        dataWork = WebHelper.getWebInterface()
                .getHomeIndex(request)
                .compose(new WebTransformer<>(this))
                .subscribe(new WebSubscriber() {
                    @Override
                    public void onSuccess(IndexResponse response) {
                        parseData(response);
                    }
                });

注意要在onDestroy中解绑:

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (dataWork != null) {
            dataWork.isUnsubscribed();
        }
    }

4.在WebTransformer中添加进度条和线程切换

public class WebTransformer implements Observable.Transformer {
    Context context;
    boolean hasDialog = true;

    public WebTransformer(Context context) {
        this.context = context;
    }

    public WebTransformer(Context context, boolean hasDialog) {
        this.context = context;
        this.hasDialog = hasDialog;
    }

    @Override
    public Observable call(Observable observable) {
        return observable.subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        if (hasDialog) {
                            DialogHelper.showDialog(context);
                        }
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread());
    }
}

5.在WebSubscriber中添加统一的错误处理

public abstract class WebSubscriber extends Subscriber {

    @Override
    public void onCompleted() {
    }

    @Override
    public void onNext(T t) {
        DialogHelper.hideDialog();
        //居然还有的返回值不是继承BaseResponse,我想杀了后台,别拦我
        if (t instanceof BaseResponse) {
            BaseResponse baseResponse = (BaseResponse) t;
            //不要问我为什么这么多code,我也不知道后台怎么想的
            if (baseResponse.getCode() != 0 || baseResponse.getResultCode() != 0 || baseResponse.getReplyCode() != 0) {
                String tip = "";
                //不要问我为什么这么多message,我也不知道后台怎么想的
                if (!TextUtils.isEmpty(baseResponse.getMessage())) {
                    tip = baseResponse.getMessage();
                } else {
                    tip = baseResponse.getRemark();
                }
                ToastHelper.show(tip);
                onFailed(t);
            } else {
                onSuccess(t);
            }
        } else {
            onSuccess(t);
        }
    }

    @Override
    public void onError(Throwable e) {
        DialogHelper.hideDialog();
        if (e instanceof HttpException) {
            ToastHelper.show("网络异常");
            HttpException exception = (HttpException) e;
            if (exception.code() == 404) {
                //没登陆的时候居然返回404,我想杀了后台,别拦我
                SkipManager.gotoLogin();
            }
        }
    }

    //需要个性化的错误处理时,重写它
    protected void onFailed(T t) {

    }

    //正确的回调
    public abstract void onSuccess(T t);

}

2.Retrofit的优点

Q1.什么是网络框架?
业务层-->网络调度层-->网络执行层

这里:
Retrofit是网络调度层
类似volley, retrofit, android-async-http
做具体业务请求、线程切换、数据转换

OkHttp是网络执行层
类似HttpClient, HttpUrlConnection
做底层网络请求

Q2.为什么用Retrofit?
因为Retrofit有:
CallAdapter-请求适配器
可以实现多种请求响应形式:同步方式、异步回调方式、RxJava方式
Converter-数据转换器
可以自己定义responseBodyConverter和requestBodyConverter,实现加密功能和各种奇葩格式的解析

你可能感兴趣的:(Android基础)