引入依赖
implementation "io.reactivex.rxjava3:rxjava:3.0.0"
基本使用
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.disposables.Disposable;
private static void simpleObservableObserver() {
System.out.println("SimpleRxJava.simpleObservableObserver");
Observable<String> sender = Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(@NonNull ObservableEmitter<String> emitter) throws Throwable {
emitter.onNext("Hello");
emitter.onNext("RxJava");
emitter.onNext("!");
emitter.onComplete();
}
});
Observer<String> receiver = new Observer<String>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
System.out.println("SimpleRxJava.onSubscribe");
System.out.println("d = " + d);
}
@Override
public void onNext(@NonNull String s) {
System.out.println("SimpleRxJava.onNext");
System.out.println("s = " + s);
}
@Override
public void onError(@NonNull Throwable e) {
System.out.println("SimpleRxJava.onError");
System.out.println("e = " + e);
}
@Override
public void onComplete() {
System.out.println("SimpleRxJava.onComplete");
}
};
sender.subscribe(receiver);
}