Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve symbol Obserable.onSubscribe in rxjava

as i am new to RxJava trying to run following code but its showing me

Cannot resolve symbol `Obserable.onSubscribe`

code is as follow

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

have added following entries in gradle

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.8'

for java 8 compatibility

 compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
like image 476
Hunt Avatar asked Jan 02 '23 23:01

Hunt


1 Answers

From the comments:

You are depending on RxJava 2 but the code example is for RxJava 1. There is no Observable.OnSubscribe in v2 and you are not supposed to call create(OnSubscribe) either as it is deprecated for being unsafe.

If you wanted an RxJava 1 dependency:

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

If you wanted RxJava 2 code:

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
            }
        }
    });
like image 194
akarnokd Avatar answered Jan 17 '23 06:01

akarnokd