Search code examples
androidrx-javarx-java2rx-android

.subscribeOn() method is not recognized


I want to use RxAndroid and RxJava in my project. So, in the build.gradle file a added the lines shown below. but, when I tried to use

            animalsObserver.subscribeOn();//unrecognized

it is not recognized by AndroidStudio. So, please let me know how to correctly use .subscribeOn() method.

code:

import rx.Observable;
import rx.Observer;

public class MainActivity extends AppCompatActivity {
    Observable<String> animalsObservable = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        animalsObservable = Observable.just("Ant", "Bee", "Cat", "Dog", "Fox");
        Observer<String> animalsObserver = getAnimalsObserver();
        animalsObserver.subscribeOn();//unrecognized
    }

    private Observer<String> getAnimalsObserver() {
        return new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "onSubscribe");
            }

            @Override
            public void onNext(String s) {
                Log.d(TAG, "Name: " + s);
            }

            @Override
            public void onError(Throwable e) {
                Log.e(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "All items are emitted!");
            }
        };
    }
}

gradle:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.9'
testCompile 'junit:junit:4.12'

}


Solution

  • It's Observable that has subcribeOn() api, not Observer. Your example should be something like:

    animalsObservable.subscribeOn(AndroidSchedulers.mainThread()).subscribe(getAnimalsObserver())