1、使用Observable.range(int start, int count)创建一个发射特定整数序列的Observable,第一个参数为起始值,第二个为发送的个数,如果为0则不发送,负数则抛异常。
import rx.Observable; import rx.Subscriber; public class MyDemo { public static void main(String[] args) { //创建发射源(被观察者) Observable<Integer> observable = Observable.range(1, 5); //创建接收源(观察者) Subscriber<Object> subscriber = new Subscriber<Object>() { @Override public void onStart() { System.out.println("Subscriber onStart: "); } @Override public void onCompleted() { System.out.println("Subscriber onCompleted: "); } @Override public void onError(Throwable e) { System.out.println("Subscriber onError: " + e.toString()); } @Override public void onNext(Object t) { System.out.println("Subscriber onNext: " + t); } }; //订阅 observable.subscribe(subscriber); } }
2、Subscriber的简化
RxJava提供了Action0等一系列的类似的类,当我们不需要(或者不关心)Subscriber的全部方法(如只关心onNext方法),就可以使用Action类来替代Subscriber。
2.1、当我们并不关心OnComplete和OnError,可以使用 subscribe(onNextAction),我们只需要在onNext的时候做一些处理,这时候就可以使用Action1类:
import rx.Observable; import rx.functions.Action1; public class MyDemo { public static void main(String[] args) { Person person = new Person(21, "js"); //创建发射源(被观察者) Observable<Person> observable = Observable.just(person); //订阅,创建接收源(观察者) observable.subscribe(new Action1<Person>() { @Override public void call(Person t) { System.out.println("Action1 call:" + t.toString()); } }); } private static class Person{ private int age; private String name; public Person(int age, String name) { this.age = age; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person[age=" + age + ",name=" + name + "]" ; } } }
2.2、subscribe方法还有两个重载方法,分别是:
- 两个参数: subscribe(onNextAction , onErrorAction)
- 三个参数: subscribe(onNextAction , onErrorAction ,onCompletedAction)
import rx.Observable; import rx.functions.Action0; import rx.functions.Action1; public class MyDemo { public static void main(String[] args) { Person person = new Person(21, "js"); //创建发射源(被观察者) Observable<Person> observable = Observable.just(person); //订阅,创建接收源(观察者) Action1<Person> onNextAction = new Action1<Person>() { @Override public void call(Person t) { System.out.println("Action1 call:" + t.toString()); } }; Action1<Throwable> onErrorAction = new Action1<Throwable>() { @Override public void call(Throwable t) { System.out.println("Action1 call:" + t.toString()); } }; Action0 onCompletedAction = new Action0() { @Override public void call() { System.out.println("Action0 call"); } }; observable.subscribe(onNextAction, onErrorAction); observable.subscribe(onNextAction, onErrorAction, onCompletedAction); } private static class Person{ private int age; private String name; public Person(int age, String name) { this.age = age; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person[age=" + age + ",name=" + name + "]" ; } } }