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<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
subscriber.onNext("hello word");
subscriber.onCompleted();
}
});
//2创建订阅者
Subscriber subscriber = new Subscriber<String>() {
@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
网友评论