Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve method SubscribeOn - RxJava

I am very new to RxJava, I am trying to make Retrofit calls using RxJava. When I write this code on SubscribeOn it says 'Cannot resolve method SubscribeOn(io.reactivex.scheduler)'.

Could you guide on where I am doing it wrong.

Thanks R

getDemoData in the Presenter layer.

  void getDemoData(){
        mCompositeDisposable.add(apiInterface.getDemoData()
                //subscribeOn has the error.
                .subscribeOn(Schedulers.io()) // "work" on io thread
                .observeOn(AndroidSchedulers.mainThread()) // "listen" on UIThread
                .map(new Function<IApiCalls, List<DemoJSONAPIData>>() {
                    @Override
                    public List<DemoJSONAPIData> apply(
                            @io.reactivex.annotations.NonNull final IApiCalls apiCalls)
                            throws Exception {
                        // we want to have the geonames and not the wrapper object
                        return apiCalls.getDemoData();
                    }
                })
                .subscribe(new Consumer<List<Geoname>>() {
                    @Override
                    public void accept(
                            @io.reactivex.annotations.NonNull final List<Geoname> geonames)
                            throws Exception {
                        //display
                    }
                })
        );
    }

ApiInterface

public interface ApiInterface {
    @GET("/posts")
    //Single<DemoJSONAPIData> getDemoData();
    Call<List<DemoJSONAPIData>> getDemoData();
}

IApiCalls

public interface IApiCalls {
    List<DemoJSONAPIData> getDemoData();
}

ApiClient

public class ApiClient {
    public static final String BASE_URL_TWO = "https://jsonplaceholder.typicode.com";
    public static Retrofit retrofit = null;
    public  static Retrofit getApiClient()
    {
        if(retrofit == null)
        {
            retrofit = new Retrofit.Builder().baseUrl(BASE_URL_TWO)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 
                    .build();
        }
        return retrofit;
    }
}

dependencies

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:converter-jackson:2.2.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'

Edit

My Retrofit call which I want to implement using RxJava/rxAndroid

 @Override
    public List<DemoJSONAPIData> getDemoData() {
        try{
            demoJSONAPIDatas = new ArrayList<>();
            apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
            Call<List<DemoJSONAPIData>> call = apiInterface.getDemoData();
            call.enqueue(new Callback<List<DemoJSONAPIData>>() {
                @Override
                public void onResponse(Call<List<DemoJSONAPIData>> call, Response<List<DemoJSONAPIData>> response) {
                    Log.d("MainActivity", "Status Code = " + response.code());
                    demoJSONAPIDatas = response.body();
                    Log.d("demoJSONAPIDatas", demoJSONAPIDatas.toString());
                    for(DemoJSONAPIData demoJSONAPIData: demoJSONAPIDatas){
                        Log.d("UserId", demoJSONAPIData.getId());
                        Log.d("Title", demoJSONAPIData.getTitle());
                    }
                }
                @Override
                public void onFailure(Call<List<DemoJSONAPIData>> call, Throwable t) {
                    Log.d("error", "error");
                }
            });
        }catch (Exception e){
            System.out.println("Error " + e.getMessage());
        }
        return demoJSONAPIDatas;
    }

Error when using Single. enter image description here

like image 361
BRDroid Avatar asked Apr 19 '26 15:04

BRDroid


2 Answers

Check your import for observable in ApiInterface. it should be import io.reactivex.Observable;

like image 130
Sonika Avatar answered Apr 21 '26 03:04

Sonika


This happened to me too. So when I go check my APIinterface, it has automatically imported the 'import android.database.Observable;' instead of 'import io.reactivex.Observable;'. When u change it to that all the errors were gone.

like image 28
maneesha Avatar answered Apr 21 '26 05:04

maneesha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!