RxJava入门,创建一个hello world(一)

1.首先导入对于的包

 //rxjava
    compile 'io.reactivex:rxjava:1.1.6'
    compile 'io.reactivex:rxandroid:1.2.1'

2.在Activity中创建以下代码,可以看到运行结果

//1创建被订阅者
        Observable observable = Observable.create(new Observable.OnSubscribe() {
            @Override
            public void call(Subscriber subscriber) {
                subscriber.onNext("hello word");
                subscriber.onCompleted();
            }
        });

        //2创建订阅者
        Subscriber subscriber = new Subscriber() {
            @Override
            public void onCompleted() {
                System.out.println("onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("onError");
            }

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

        //3事件订阅
        observable.subscribe(subscriber);
image.png

你可能感兴趣的:(RxJava入门,创建一个hello world(一))