Rxjava的基本使用

时间:2023-01-13 19:37:05

gradle配置:

    compile 'io.reactivex:rxjava:1.0.9'

观察者:

    Observer<String> observer = new Observer<String>() {
@Override
public void onNext(String s) {
Log.d(tag, s);
} @Override
public void onCompleted() {
Log.d(tag, "Completed!");
} @Override
public void onError(Throwable e) {
Log.d(tag, "Error!");
}
};

被观察者:

 Observable observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
subscriber.onNext("Hello");
subscriber.onNext("Hi");
subscriber.onNext("Aloha");
subscriber.onCompleted();
}
});

订阅:

observable.subscribe(observer);