RxJava打印Hello World!

import rx.Observable;
import rx.Subscriber;

public class HelloWorld {
    public static void main(String args[]) {
        //第一步创建被观察者
        Observable myObservable = Observable.create(new Observable.OnSubscribe() {
            public void call(Subscriber subscriber) {
                subscriber.onNext("hello world!");
                subscriber.onNext("hello Derran!");
                subscriber.onCompleted();
            }
        });

        //创建观察者
        Subscriber subscriber = new Subscriber() {
            public void onCompleted() {
                System.out.println("onCompleted() ");
            }

            public void onError(Throwable throwable) {
                System.out.println("onError() ");
            }

            public void onNext(String o) {
                System.out.println("onNext():" + o);
            }
        };

        //订阅事件
        myObservable.subscribe(subscriber);
    }
}

onCompleted()与onError()不能同时执行!

你可能感兴趣的:(RxJava打印Hello World!)