本篇博客将会了解RxJava的操作符,主要体现在数据的展示或者传递方面
打印数组:
String[] data = {"1", "2", "3"}; Observable.from(data).subscribe(new Observer<String>() { @Override public void onCompleted() { LogUtils.d("onCompleted():" + Thread.currentThread().getName()); } @Override public void onError(Throwable e) { LogUtils.d("onError():" + Thread.currentThread().getName()); } @Override public void onNext(String s) { LogUtils.d("onNext():data:" + s + " 线程" + Thread.currentThread().getName()); } });
12-25 03:27:52.341 13309-13309/com.rxandroid.test1 D/----->: onNext():data:1 线程main
12-25 03:27:52.341 13309-13309/com.rxandroid.test1 D/----->: onNext():data:2 线程main
12-25 03:27:52.341 13309-13309/com.rxandroid.test1 D/----->: onNext():data:3 线程main
12-25 03:27:52.341 13309-13309/com.rxandroid.test1 D/----->: onCompleted():main
简洁打印多个对象:
Observable.just("1", "2", "3").subscribe(new Observer<String>() { @Override public void onCompleted() { LogUtils.d("onCompleted():" + Thread.currentThread().getName()); } @Override public void onError(Throwable e) { LogUtils.d("onError():" + Thread.currentThread().getName()); } @Override public void onNext(String s) { LogUtils.d("onNext():" + Thread.currentThread().getName()); } });
12-25 03:36:57.011 27949-27949/com.rxandroid.test1 D/----->: onNext():main
12-25 03:36:57.011 27949-27949/com.rxandroid.test1 D/----->: onNext():main
12-25 03:36:57.011 27949-27949/com.rxandroid.test1 D/----->: onNext():main
12-25 03:36:57.011 27949-27949/com.rxandroid.test1 D/----->: onCompleted():main
打印集合:
List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); Observable.from(list).subscribe(new Observer() { @Override public void onCompleted() { LogUtils.d("onCompleted():" + Thread.currentThread().getName()); } @Override public void onError(Throwable e) { LogUtils.d("onError():" + Thread.currentThread().getName()); } @Override public void onNext(Object o) { LogUtils.d("onNext():data:" + o + " 线程" + Thread.currentThread().getName()); } });
打印结果:
12-25 03:39:51.001 27949-27949/com.rxandroid.test1 D/----->: onNext():data:1 线程main
12-25 03:39:51.001 27949-27949/com.rxandroid.test1 D/----->: onNext():data:2 线程main
12-25 03:39:51.001 27949-27949/com.rxandroid.test1 D/----->: onNext():data:3 线程main
12-25 03:39:51.001 27949-27949/com.rxandroid.test1 D/----->: onCompleted():main
打印观察者集合:
List<Observable<String>> data = new ArrayList<Observable<String>>(); // data.add(Observable.just("1","2","3")); data.add(Observable.just("4", "5", "6")); // an Iterable of Observable sources competing to react first Observable.amb(data).subscribe(new Observer<String>() { @Override public void onCompleted() { LogUtils.d("onCompleted():" + Thread.currentThread().getName()); } @Override public void onError(Throwable e) { LogUtils.d("onError():" + Thread.currentThread().getName()); } @Override public void onNext(String o) { LogUtils.d("onNext():data:" + o + " 线程" + Thread.currentThread().getName()); } });
12-25 03:41:19.551 27949-27949/com.rxandroid.test1 D/----->: onNext():data:4 线程main
12-25 03:41:19.551 27949-27949/com.rxandroid.test1 D/----->: onNext():data:5 线程main
12-25 03:41:19.551 27949-27949/com.rxandroid.test1 D/----->: onNext():data:6 线程main
12-25 03:41:19.551 27949-27949/com.rxandroid.test1 D/----->: onCompleted():main
打印合并Observable:
//an Observable to be concatenated 合并两个或者多个 Observable.concat(Observable.just("1", "2", "3"), Observable.just("4", "5", "6")).subscribe(new Observer<String>() { @Override public void onCompleted() { LogUtils.d("onCompleted():" + Thread.currentThread().getName()); } @Override public void onError(Throwable e) { LogUtils.d("onError():" + Thread.currentThread().getName()); } @Override public void onNext(String o) { LogUtils.d("onNext():data:" + o + " 线程" + Thread.currentThread().getName()); } }); }
12-25 03:42:49.311 27949-27949/com.rxandroid.test1 D/----->: onNext():data:1 线程main
12-25 03:42:49.311 27949-27949/com.rxandroid.test1 D/----->: onNext():data:2 线程main
12-25 03:42:49.311 27949-27949/com.rxandroid.test1 D/----->: onNext():data:3 线程main
12-25 03:42:49.311 27949-27949/com.rxandroid.test1 D/----->: onNext():data:4 线程main
12-25 03:42:49.311 27949-27949/com.rxandroid.test1 D/----->: onNext():data:5 线程main
12-25 03:42:49.311 27949-27949/com.rxandroid.test1 D/----->: onNext():data:6 线程main
12-25 03:42:49.311 27949-27949/com.rxandroid.test1 D/----->: onCompleted():main
新技术交流群:476899320