9

Subscriber尝试创建和订阅时,我收到如下错误消息。

can not resolve method 'subscribe(anonymous rx.Subscriber<GooglePlacesResponse>)'

构建.gradle

// JSON Parsing
compile 'com.google.code.gson:gson:2.6.1'
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.2'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1

GooglePlaceService.java

public interface GooglePlaceService {

    public static final String GOOGLE_API_KEY = "google_api_key";

    @GET("maps/api/place/nearbysearch/json?radius=2000&key="+GOOGLE_API_KEY)
    Observable<GooglePlacesResponse> getNearbyPlaces(@Query("location") String location);
}

ApiUtils.java

public class ApiUtils {    

    public static final String GOOGLE_PLACE_BASE_URL = "https://maps.googleapis.com/";

    public static GooglePlaceService getGooglePlaceService() {
        return getClient(GOOGLE_PLACE_BASE_URL).create(GooglePlaceService.class);
    }

    public static Retrofit getClient(String baseUrl) {

        Retrofit retrofit = new Retrofit.Builder()
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();

        return retrofit;
    }
}

ObservableObservable<GooglePlacesResponse>如下。

Observable<GooglePlacesResponse> mGooglePlacesResponseObervable = ApiUtils.getGooglePlaceService().getNearbyPlaces(latitude + "," + longitude);

mGooglePlacesResponseObervable
   .subscribeOn(Schedulers.io())
   .observeOn(AndroidSchedulers.mainThread())
   .subscribe(new Subscriber<GooglePlacesResponse>() { <-- Error here : can not resolve method `subscribe(anonymous rx.Subscriber<GooglePlacesResponse>)`

            @Override
            public void onNext(GooglePlacesResponse googlePlacesResponse) {

                }

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }
     });
4

4 回答 4

14

适配器具有版本的事实2.*.*并不意味着它旨在与 RxJava 2 一起使用

您应该使用RxJava第二版的官方适配器:

implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' // works with RxJava 2

然后你可以添加工厂:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.example.com")
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .build();

这是完整的答案

于 2017-01-20T08:15:00.923 回答
11

RxJavaCallAdapter返回一个 RxJava 1 Observable。你应该使用RxJava2CallAdapterRxJava2。看起来这还没有在官方改造版本中,但在 2.1.1 快照中。您可以自己编译适配器,也可以从 sonatype 快照存储库中提取依赖项。

将以下内容添加到您的repositories部分build.gradle-

repositories {
    // Other repos...
    maven {
        url = "https://oss.sonatype.org/content/repositories/snapshots/"
    }
}

将您的改造依赖项更新到2.1.1-SNAPSHOT版本。请注意,我们还更改adapter-rxjavaadapter-rxjava2——

compile 'com.squareup.retrofit2:retrofit:2.1.1-SNAPSHOT'
compile 'com.squareup.retrofit2:converter-gson:2.1.1-SNAPSHOT'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.1.1-SNAPSHOT'

并更新您的改造建造者以使用RxJava2CallAdapterFactory——

Retrofit retrofit = new Retrofit.Builder()
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(baseUrl)
            .build();

当 2.1.1 发布时,您可以回到常规依赖项。

于 2016-12-24T22:58:48.237 回答
4

这是我的 build.gradle 设置,也许这​​对其他人有帮助

depencies{
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.okhttp3:okhttp:3.8.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'}
于 2017-09-20T08:50:33.780 回答
1

如果您不想更新改造版本。是一个非常好的库,可以与对象相互RxJava1.x转换RxJava2.x。首先,将此添加
compile "com.github.akarnokd:rxjava2-interop:0.10.2"
build.gradle并使用方法
RxJavaInterop.toV2Observable(observableRx1)
转换为 Rx2 Observables。

于 2017-08-04T21:34:02.187 回答