Android RxJava小结

吾生也有涯,而知也无涯。这篇文章主要讲述Android RxJava小结相关的知识,希望能为你提供帮助。
一、如何使用在build.gradle中添加依赖

1 dependencies { 2api ‘io.reactivex:rxandroid:1.2.1‘ 3api ‘io.reactivex:rxjava:1.3.0‘ 4implementation fileTree(include: [‘*.jar‘], dir: ‘libs‘) 5implementation ‘com.android.support:appcompat-v7:27.1.1‘ 6implementation ‘com.android.support.constraint:constraint-layout:1.1.3‘ 7testImplementation ‘junit:junit:4.12‘ 8androidTestImplementation ‘com.android.support.test:runner:1.0.2‘ 9androidTestImplementation ‘com.android.support.test.espresso:espresso-core:3.0.2‘ 10 }

【Android RxJava小结】这里有一个小坑,直接用latest.release没有办法用,不知道为什么
二、代码实现 2.1 使用just+Action1+Action0来实现
1Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 2.subscribeOn(Schedulers.io()) // Observable运行的线程 3.observeOn(AndroidSchedulers.mainThread()) //监听者运行的线程 4.subscribe(new Action1< Integer> () { 5//onNext 6@Override 7public void call(Integer integer) { 8log("call 1:" + integer); 9} 10}, new Action1< Throwable> () { 11//onError 12@Override 13public void call(Throwable throwable) { 14log("call 2"); 15} 16}, new Action0() { 17//onCompleted 18@Override 19public void call() { 20log("call 3"); 21} 22});

运行结果如下,很简单,就不一一解释了。
1 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:1 2 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:2 3 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:3 4 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:4 5 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:5 6 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:6 7 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:7 8 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:8 9 10-05 11:05:45.956 23619 23619 E MainActivity: yanlog msg:call 1:9 10 10-05 11:05:45.956 23619 23619 E MainActivity: yanlog msg:call 1:10 11 10-05 11:05:45.956 23619 23619 E MainActivity: yanlog msg:call 3

  2.2 使用Just+Subscriber来实现代码如下:
Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) .subscribeOn(Schedulers.io()) // Observable运行的线程 .observeOn(AndroidSchedulers.mainThread()) //监听者运行的线程 .subscribe(new Subscriber< Integer> () { @Override public void onCompleted() { log("onCompleted"); }@Override public void onError(Throwable e) { log("onError"); }@Override public void onNext(Integer integer) { log("onNext:" + integer); } });

运行结果如下:
1 10-05 19:56:09.991982982 E MainActivity: yanlog msg:onNext:1 2 10-05 19:56:09.991982982 E MainActivity: yanlog msg:onNext:2 3 10-05 19:56:09.991982982 E MainActivity: yanlog msg:onNext:3 4 10-05 19:56:09.992982982 E MainActivity: yanlog msg:onNext:4 5 10-05 19:56:09.992982982 E MainActivity: yanlog msg:onNext:5 6 10-05 19:56:09.992982982 E MainActivity: yanlog msg:onNext:6 7 10-05 19:56:09.992982982 E MainActivity: yanlog msg:onNext:7 8 10-05 19:56:09.992982982 E MainActivity: yanlog msg:onNext:8 9 10-05 19:56:09.992982982 E MainActivity: yanlog msg:onNext:9 10 10-05 19:56:09.992982982 E MainActivity: yanlog msg:onNext:10 11 10-05 19:56:09.992982982 E MainActivity: yanlog msg:onCompleted

 

    推荐阅读