我正在android中学习rxJava,因此非常新。我正在使用以下网站的示例。
https://www.androidhive.info/RxJava/rxjava-understanding-observables/
请参阅以下示例。以下是我的代码
Flowable<Integer> flowableObservable = getFlowableObservable();
SingleObserver<Integer> observer = getFlowableObserver();
flowableObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
//////////////////
private Flowable<Integer> getFlowableObservable() {
return Flowable.range(1, 100);
}
private SingleObserver<Integer> getFlowableObserver() {
return new SingleObserver<Integer>() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG, "onSubscribe");
// disposable = d;
}
@Override
public void onSuccess(Integer integer) {
Log.d(TAG, "onSuccess: " + integer);
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e.getMessage());
}
};
}
所以这给了我一行错误
SingleObserver<Integer> observer = getFlowableObserver();
flowableObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
错误是无法解决订阅方法
我正在使用的图书馆是
编译组:'io.reactivex.rxjava2',名称:'rxjava',版本:'2.1.9' 实现'io.reactivex.rxjava2:rxandroid:2.0.1
那是什么原因,我相信我已经从该站点复制了几乎相同的代码? 谢谢。
答案 0 :(得分:3)
您对source-consumer types的使用有些混乱:Flowable
需要一个Subscriber
。试试这个:
Flowable<Integer> flowableObservable = getFlowableObservable();
// vvvvvvvvvvvvvvvvvvv ------------------------------------------------
Subscriber<Integer> observer = getFlowableObserver();
flowableObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
private Flowable<Integer> getFlowableObservable() {
return Flowable.range(1, 100);
}
private Subscriber<Integer> getFlowableObserver() {
// vvvvvvvvvv ---------------------------------------------
return new Subscriber<Integer>() {
@Override
public void onSubscribe(Subscription d) {
Log.d(TAG, "onSubscribe");
// request is required to start items flowing!
// vvvvvvvvvvvvvvvvvvvvvvvvv --------------------------------
d.request(Long.MAX_VALUE);
}
@Override
// vvvvvv --------------------------------------------
public void onNext(Integer integer) {
Log.d(TAG, "onSuccess: " + integer);
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e.getMessage());
}
@Override
// vvvvvvvvvv ----------------------------------------
public void onComplete(Throwable e) {
Log.e(TAG, "onComplete");
}
};
}
答案 1 :(得分:1)
这是您的代码:
SELECT Company, AVG(Sales)
FROM CSVDatabase.dbo.sales_info
GROUP BY Company
与教程中的代码不同:
SingleObserver<Integer> observer = getFlowableObserver();
flowableObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
方法observeOn返回一个Flowable,因此,如果您紧随其后调用flowableObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.reduce(0, new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer result, Integer number) {
//Log.e(TAG, "Result: " + result + ", new number: " + number);
return result + number;
}
})
.subscribe(observer);
,则与执行此操作相同:
subscribe
如果您使用reduce,它会
Flowable<Integer> resultOfObserveOn = flowableObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
resultOfObserveOn.subscribe(observer); //this is the subscribe method from the Flowable class