Retrofit2 + RxJava2 + RxAndroid错误

9
当我尝试创建Subscriber并订阅时,我收到以下错误消息。
can not resolve method 'subscribe(anonymous rx.Subscriber<GooglePlacesResponse>)'

build.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;
    }
}

Observable Observable<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个回答

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();

这是完整答案


这真的救了我。 - Jimmy Ilenloa

11

RxJavaCallAdapter 返回一个 RxJava 1 的 Observable。您应该使用 RxJava2CallAdapter 来支持 RxJava2。看起来这个适配器还没有在 Retrofit 的正式版本中发布,但是在 2.1.1 快照版中已经有了。您可以要么自己编译适配器,要么从 Sonatype 快照仓库中下载依赖项。

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

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

将您的Retrofit依赖更新到2.1.1-SNAPSHOT版本。请注意,我们还将adapter-rxjava更改为adapter-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'

并更新你的retrofit构建器以使用RxJava2CallAdapterFactory --

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

当2.1.1版本发布后,您可以回到常规的依赖关系。


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'}

1
如果您不想更新Retrofit版本,这里有一个非常好的库可用于将RxJava1.xRxJava2.x对象之间进行转换。首先,在build.gradle中添加以下内容:
compile "com.github.akarnokd:rxjava2-interop:0.10.2"
然后使用方法RxJavaInterop.toV2Observable(observableRx1)来转换为Rx2 Observables。请注意,保留HTML标签。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接