全体像を掴むために、このビデオは分かりやすくていいと思います!
まずは、最小構成
Observable<String> myObservable = Observable.just("Some new string", "Some other String", "Another");
// "s" is a subscribed string, and logging it when received in onNext
myObservable.subscribe(s -> Log.d(TAG, "Logging: " + s));
Observerを設定し、処理を記述することもできる
myObserver = new Observer<String>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
Log.i(TAG, "onSubscribe invoked");
}
@Override
public void onNext(@NonNull String s) {
Log.i(TAG, "onNext invoked");
Log.i(TAG, "Logging " + s);
}
@Override
public void onError(@NonNull Throwable e) {
Log.i(TAG, "onError invoked");
}
@Override
public void onComplete() {
Log.i(TAG, "onComplete invoked");
}
};
myObservable.subscribe(myObserver);