0

I replaced the action call to Consumer in my code but while subscribing it keeps asking me to cast it to observer

Below is the code

public void fetchSubscriptionPlans(String url, String apiKey, String authToken,
                                   final Consumer<List<ContentDatum>> subscriptionPlans) {
    appCMSSubscriptionPlanRest.getPlansById(url,authHeaders).enqueue(new Callback<List<ContentDatum>>() {
        @Override
        public void onResponse(Call<List<ContentDatum>> call, Response<List<ContentDatum>> response) {
            try {

                Observable.just(response.body())
                        .onErrorResumeNext(throwable -> Observable.empty())
                        .subscribe(subscriptionPlans);
            } catch (Exception e) {
                Observable.just((List<ContentDatum>) null)
                        .onErrorResumeNext(throwable -> Observable.empty())
                        .subscribe(subscriptionPlans);
            }
        }

        @Override
        public void onFailure(Call<List<ContentDatum>> call, Throwable t) {

        }
    });
}

I get the error on .subscribe(subscriptionPlans);to cast it as .subscribe((Observer<? super List<ContentDatum>>) subscriptionPlans);

What should be correct way?

And on running the code I get the exception

cannot be cast to rx.Observer
WISHY
  • 11,067
  • 25
  • 105
  • 197
  • What's the actual error you get? The suggestion to cast to `Observer` isn't the error - it's a suggestion to fix the error. – dano Jul 09 '20 at 14:50
  • @dano I am getting exception cannot be cast to rx.Observer – WISHY Jul 10 '20 at 07:03

2 Answers2

0

First, be sure that your consumer is of type : io.reactivex.functions.Consumer

Second, I think that your response.body() returns not a List of ContentDatum.

I've the same error when doing the following :

Consumer<Integer> subscriptionPlans = list -> {};

Observable.just("Hello")
        .onErrorResumeNext((Throwable throwable) -> Observable.empty())
        .subscribe(subscriptionPlans);

enter image description here

bubbles
  • 2,597
  • 1
  • 15
  • 40
  • I have tried with both type of Consumers io.reactivex.functions.Consumer; and io.reactivex.rxjava3.functions.Consumer; doesn't work out. Also response.body() does returns the list of contentDatum – WISHY Jul 09 '20 at 14:45
  • 1
    yes I didn't realise that you're using v3 of rxjava ... it seems that you found the solution – bubbles Jul 10 '20 at 09:57
0

I had the wrong import for Observable. Have to use this

io.reactivex.rxjava3.core.Observable
WISHY
  • 11,067
  • 25
  • 105
  • 197