Observer(新订阅者<xxx>()...)对Observer对象不起作用Retrofit-RxJava

时间:2017-01-08 18:56:10

标签: android observable retrofit2 rx-java2

我正在关注本教程https://youtu.be/YoSr5mi5kKU?t=30m52s,以了解Android中的RxJava和MVP模式。

但是当它到达观察物体的时刻时,现在可行了:

public void getDataTMDBinteractor() {
    Map<String, String> params = new HashMap<>();
    params.put("api_key", "zzzzzzzzzzzzzzzzzzzzzzzzzzz");
    Observable<ResponseTMDB> responseTMDBObservable = serviceTMDB.getDataTMBDService(params);

    responseTMDBObservable.subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Subscriber<ResponseTMDB>() { //Starting error from here
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(ResponseTMDB responseTMDB) {

            }
        });
}

enter image description here

错误是:无法解析方法'subscribe(anonymous rx.Subscriber)'

服务界面是这样的:

public interface ServiceTMDB {
    @GET("movie/popular")
    Observable<ResponseTMDB> getDataTMBDService(@QueryMap Map<String, String> params);
}

我不知道究竟是什么遗漏或做得不好。

Gradle进口:

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.google.code.gson:gson:2.8.0'

2 个答案:

答案 0 :(得分:2)

简单问题,您正在添加

compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

适用于RxJava 1.x

但是

compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

你应该添加

compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

答案 1 :(得分:0)

您正在添加:

compile 'io.reactivex:rxandroid:1.2.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'