Search code examples
androidrx-javarx-java2rx-android

error casting observable.subscribe


As shown in the following code, I am rceiving an error regarding the following line, because the casting is not correct

.subscribe((Consumer<? super List<String>>)getAnimalsObserver());

please let me know how to fix this issue.

note:

I am using the folloing rx versions
compile 'io.reactivex.rxjava2:rxandroid:2.0.2'
compile 'io.reactivex.rxjava2:rxjava:2.0.5'

code:

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

getAnimalsObservable()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe((Consumer<? super List<String>>)getAnimalsObserver());



 private Observer<String> getAnimalsObserver() {
    return new Observer<String>() {

        @Override
        public void onSubscribe(Disposable d) {
            Log.i(TAG, "onSubscribe->d: " + d);
        }

        @Override
        public void onNext(String s) {
            Log.i(TAG, "onNext->s: " + s);
        }

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

        @Override
        public void onComplete() {
            Log.i(TAG, "onComplete");
        }
    };
}  

logcat:

Caused by: java.lang.ClassCastException: 
com.example.pc_a.myapplication.MainActivity$1 cannot be cast to 
io.reactivex.functions.Consumer
at com.example.pc_a.myapplication.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6288)
at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
t android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2642)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2754) 

Solution

  • First, you don't need to cast to Consumer. Secondly, you should modify your Observer as follows to make it work (Observer already can be used in subscribe). I simplified it a bit to make it neater:

    private Observable<List<String>> getAnimalsObservable() {
        return Observable.fromArray(Arrays.asList(
            new String[]{
                "Ant", "Ape",
                "Bat", "Bee", "Bear", "Butterfly",
                "Cat", "Crab", "Cod",
                "Dog", "Dove",
                "Fox", "Frog"
            }
        ));
    }
    
    private Observer<List<String>> getObserver() {
        return new Observer<List<String>>() {
            @Override
            public void onSubscribe(Disposable d) {
                //your logic here
            }
    
            @Override
            public void onNext(List<String> values) {
                //your logic here
                for (String value : values) {
                    System.out.print(value + " ");
                }
            }
    
            @Override
            public void onError(Throwable e) {
                //your logic here
            }
    
            @Override
            public void onComplete() {
                //your logic here
            }
        };
    }
    
    @Test
    public void test() {
        // here you can add your observeOn and subscribeOn but they're not important for the test
        getAnimalsObservable()
            .subscribe(getObserver());
    }
    

    Output:

    Ant Ape Bat Bee Bear Butterfly Cat Crab Cod Dog Dove Fox Frog