OKHttp+Retrofit+RxJava
1.1、使用要求

OkHttp支持Android 2.3及其以上版本。

对于JavaJDK1.7以上。

1.2、在build.gradle里面添加依赖
compile 'com.squareup.okhttp3:okhttp:3.7.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'

compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.0'
1.3、配置Retrofit

单独使用Retrofit的时候,接口的返回值是retrofit2.Call<T>。 而我们与RxJava一起使用,为了进一步简化,我们直接将返回值改成io.reactivex.Observable<T>, 但是这一改动需要做一个设置,这个设置如下:

Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build();

这也就是我们依赖compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'的原因。

1.4、示例

假设,我们有如下的HTTP接口:

GET /updateApp HTTP/1.1

响应体如下:

{
    "businessId": "updateApp",
    "statusCode": 0,
    "message": "success",
    "data": {
        "versionName": "1.1.0",
        "text": "修改了啥就是不告诉你",
        "apkUrl": "http://blog.fpliu.com/xx.apk",
        "force": true
    }
}

我们根据响应体使用AndroidStudio的插件GsonFormat自动生成实体类,如下:

public class UpdateAppBean {
    private String businessId;
    private int statusCode;
    private String message;
    private DataBean data;

    public String getBusinessId() {
        return businessId;
    }

    public void setBusinessId(String businessId) {
        this.businessId = businessId;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getMessage() {
        return message;
    }

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

    public DataBean getData() {
        return data;
    }

    public void setData(DataBean data) {
        this.data = data;
    }

    public static class DataBean {
        private String versionName;
        private String text;
        private String apkUrl;
        private boolean force;

        public String getVersionName() {
            return versionName;
        }

        public void setVersionName(String versionName) {
            this.versionName = versionName;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getApkUrl() {
            return apkUrl;
        }

        public void setApkUrl(String apkUrl) {
            this.apkUrl = apkUrl;
        }

        public boolean isForce() {
            return force;
        }

        public void setForce(boolean force) {
            this.force = force;
        }
    }
}

定义一个Java接口与HTTP接口对应,如下:

public interface HttpAPI {
    @GET("/updateApp")
    Observable<UpdateAppBean> updateApp();
}

要注意的是方法的返回值必须是io.reactivex.Observable<T>而不是retrofit2.Call<T>

接下来就是请求了,如下:

RetrofitRequest
    .getRetrofit("http://192.168.1.101")
    .create(HttpAPI.class)
    .updateApp()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Observer() {
        @Override
        public void onSubscribe(@NonNull Disposable d) {
            Log.d(TAG, "onSubscribe()");
        }

        @Override
        public void onNext(@NonNull UpdateAppBean updateAppBean) {
            Log.d(TAG, "onNext()" + updateAppBean);

            TextView textView = (TextView) findViewById(R.id.text_view);
            textView.setText(updateAppBean.getData().getText());
        }

        @Override
        public void onError(@NonNull Throwable e) {
            Log.e(TAG, "onError()", e);
        }

        @Override
        public void onComplete() {
            Log.d(TAG, "onComplete()");
        }
    });