无法在rxjava中解析符号Obserable.onSubscribe

时间:2018-01-11 08:33:24

标签: android rx-java rx-java2

因为我是RxJava的新手,试图运行以下代码,但它显示了我

Cannot resolve symbol `Obserable.onSubscribe`

代码如下

Observable<String> fetchFromGoogle = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                try {

                }catch(Exception e){
                    subscriber.onError(e); // In case there are network errors
                }
            }
        });

enter image description here

gradle

中添加了以下条目
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.8'

for java 8兼容性

 compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

1 个答案:

答案 0 :(得分:6)

来自评论:

你依赖于RxJava 2,但是代码示例是针对RxJava 1.在v2中没有Observable.OnSubscribe,你也不应该调用create(OnSubscribe),因为它因为不安全而被弃用。

如果你想要一个RxJava 1依赖:

compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.3.4'

如果你想要RxJava 2代码:

io.reactivex.Observable<String> fetchFromGoogle = io.reactivex.Observable.create(
    new ObservableOnSubscribe<String>() {
        @Override
        public void subscribe(ObservableEmitter<String> emitter) {
            try {

            } catch(Exception e) {
                emitter.onError(e); // In case there are network errors
            }
        }
    });