无法解析方法SubscribeOn-RxJava

BRD*_*oid 0 android retrofit reactivex rx-java2

我对RxJava非常陌生,我正在尝试使用RxJava进行Retrofit调用。当我在SubscribeOn上编写此代码时,它说“无法解析方法SubscribeOn(io.reactivex.scheduler)”。

你能指导我做错了什么吗?

谢谢R

Presenter层中的getDemoData。

  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
                    }
                })
        );
    }
Run Code Online (Sandbox Code Playgroud)

Api接口

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

IApiCalls

public interface IApiCalls {
    List<DemoJSONAPIData> getDemoData();
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

依存关系

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'
Run Code Online (Sandbox Code Playgroud)

编辑

我想使用RxJava / rxAndroid实现的Retrofit调用

 @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;
    }
Run Code Online (Sandbox Code Playgroud)

使用Single时出错。 在此处输入图片说明

小智 9

在ApiInterface中检查导入是否可见。它应该是import io.reactivex.Observable;