'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;
}
Solution 1:[1]
Check your import for observable in ApiInterface. it should be import io.reactivex.Observable;
Solution 2:[2]
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.
Solution 3:[3]
You probably added the wrong package.
These mistakes : " import java.util.* " --- " import android.database.Observable "
Actually:
" import io.reactivex.Observable "
You can make the necessary changes in the interface.
Solution 4:[4]
api.test(test)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.unsubscribeOn(Schedulers.io())
.subscribe(new Observer<BaseResponse>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(BaseResponse response) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
Observable<BaseResponse> test(@Field("test") String isOnline);
import Observable from rx java.
Solution 5:[5]
Just Import the following on the api interface
import rx.Observable;
and your problem will be solved
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sonika |
Solution 2 | maneesha |
Solution 3 | Arda Kazancı |
Solution 4 | Yasin Ege |
Solution 5 | user11080480 |