Retrofit2.x + rxjava2.x 入门使用 亲自踩坑

一、添加各个依赖的包

   compile 'com.squareup.retrofit2:retrofit:2.3.0'
   compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
   compile 'com.squareup.retrofit2:converter-gson:2.3.0'

   compile 'com.squareup.okhttp3:okhttp:3.8.1'

   compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
   compile 'io.reactivex.rxjava2:rxjava:2.1.0'

   compile group: 'com.google.code.gson', name: 'gson', version: '2.8.1'


   compile 'com.jakewharton:butterknife:8.7.0'
   annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0'

库的由来

1、 retrofit 的包

项目 地址 Retrofit

  1. 导入 java compile 'com.squareup.retrofit2:retrofit:2.3.0'

  2. ~/retrofit-adapters 中我们可以看到需要使用的执行默认回调的适配器。需要导入~/retrofit-adapters/rxjava2中 看到的

compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
  1. 导入~/retrofit/retrofit-converters中说到的转换器 我们使用gson所以导入~/retrofit/retrofit-converters/gson下说的
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
2、 rxjava2 +rxandroid 的包

项目地址 RxAndroid:Android

导入需要的

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
compile 'io.reactivex.rxjava2:rxjava:2.1.0'

3、 导入 gson 库

项目地址 gson

compile group: 'com.google.code.gson', name: 'gson', version: '2.8.1'

4、buttonknife库

项目地址Butter Knife
导入

compile 'com.jakewharton:butterknife:8.7.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0'

至于rxjava 和retrofit的一些基本知识建议学习作者是 扔物线的两篇文章 给 Android 开发者的 RxJava 详解和RxJava 与 Retrofit 结合的最佳实践。
不过他的里面用到的都是1.x的库,在用到2.x的时候会有一些问题。主要是retrofit2中改变了一些东西。在retrofit的项目中的wiki What's different in 2.0中官方有解释说改动了哪些地方。在其中的Subscriber部分中说更改了subscriber。这点要注意。

Due to the name conflict, replacing the package from rx to org.reactivestreams is not enough. In addition, org.reactivestreams.Subscriber has no notion of adding resources to it, cancelling it or requesting from the outside.
To bridge the gap we defined abstract classes DefaultSubscriber, ResourceSubscriber and DisposableSubscriber (plus their XObserver variants) for Flowable (and Observable) respectively that offers resource tracking support (of Disposables) just like rx.Subscriber and can be cancelled/disposed externally via dispose():


开始使用

我们使用机房监控系统的登陆接口来测试。

1、添加联网权限



2、定义返回的数据模型类

public class LoginResult {
    /**
     * success : 0
     * msg : User authorize success!
     * data : {"token":"eyJ1c2VybmFtZSI6ICJ4dXNodW4iLCAiZXhwIjogIjIwMTctMDctMjMgMDY6NTc6MTIifQ=="}
     */

    private int success;
    private String msg;
    private DataBean data;

    public int getSuccess() {
        return success;
    }

    public void setSuccess(int success) {
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public DataBean getData() {
        return data;
    }

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

    public static class DataBean {
        /**
         * token : eyJ1c2VybmFtZSI6ICJ4dXNodW4iLCAiZXhwIjogIjIwMTctMDctMjMgMDY6NTc6MTIifQ==
         */

        private String token;

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }
    }
}

3、定义一个接口

public interface LoginService {

    @FormUrlEncoded
    @POST("login")
    Observable login(@Field("username") String username, @Field("password") String password);
}

注意使用@Field 注解的时候要用@FormUrlEncoded保证他的编码格式与URL的一致,官方有说道。

4、定义一个类专门用来处理登陆的函数。

public class LoginMethod {
    public static final String BASE_LOGIN_URL = "http://*******/";
    private  static final  int DEFAULT_TIMEOUT = 5;

    private Retrofit mRetrofit;
    private  LoginService  mLoginService;

    //在访问HttpMethods时创建单例
    private static class SingletonHolder{
        private static final LoginMethod INSTANCE = new LoginMethod();
    }

    //获取单例
    public static LoginMethod getInstance(){
        return SingletonHolder.INSTANCE;
    }

    private LoginMethod(){
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

        mRetrofit = new Retrofit.Builder()
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(BASE_LOGIN_URL)
                .build();

        mLoginService  = mRetrofit.create(LoginService.class);

    }
    public void login(Observersubscriber, String uname, String psd){
        Observable observable = mLoginService.login(uname,psd);

        toSubscribe(observable,subscriber);
    }
    private  void toSubscribe(Observable o, Observer s){
        o.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe((Observer) s);
    }
}

如果要自己更多去处理异常等信息。可以自己定义gsonconverter 和变换函数等来处理信息。

5、使用上面的封装好的类。

       LoginMethod.getInstance().login(subscribe,"xushun","123456");

其中 subscribe 为;

private Observer subscribe ;
        
        
        
        subscribe = new Observer() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {

            }

            @Override
            public void onNext(@NonNull LoginResult loginResult) {
                System.out.println("tag:"+loginResult.getData().getToken());
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };

在onNext中即可以获取到服务器返回来的信息。

大功告成。师父领进门,修行在个人。继续努力。

偷偷告诉你 项目地址  retrofit2 rxjava demo

一些关于rxjava2.0 介绍的文章 关于 RxJava 最友好的文章—— RxJava 2.0 全新来袭

你可能感兴趣的:(Retrofit2.x + rxjava2.x 入门使用 亲自踩坑)