简体   繁体   English

Retrofit2 + RxJava2 + RxAndroid错误

[英]Retrofit2 + RxJava2 + RxAndroid error

I am getting error message as below when try to crate Subscriber and subscribe. 当我尝试创建Subscriber并订阅时,我收到如下错误消息。

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

build.gradle 的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 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 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> is as below. 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) {

            }
     });

The fact that adapter has version 2.*.* does not mean that it is intended for use with RxJava 2 适配器具有版本2.*.*这一事实并不意味着它适用于RxJava 2

You should use the official adapter for the second version of RxJava: 您应该将官方适配器用于第二版RxJava:

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

Then you can add factory: 然后你可以添加工厂:

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

Here is the full answer . 这是完整的答案

RxJavaCallAdapter returns a RxJava 1 Observable . RxJavaCallAdapter返回一个RxJava 1 Observable You should use RxJava2CallAdapter for RxJava2. 您应该将RxJava2CallAdapter用于RxJava2。 Looks like that is not in an official retrofit release yet, but is in the 2.1.1 snapshot. 看起来还没有正式的改版版本,但是在2.1.1快照中。 You can either compile the adapter yourself, or pull the dependencies off the sonatype snapshot repo. 您可以自己编译适配器,也可以从sonatype snapshot repo中删除依赖项。

Add the following to your repositories section in your build.gradle -- 将以下内容添加到build.gradle repositories部分 -

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

Update your retrofit dependencies to the 2.1.1-SNAPSHOT version. 将您的改进依赖项更新为2.1.1-SNAPSHOT版本。 Note that we also change adapter-rxjava to adapter-rxjava2 -- 请注意,我们还将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'

and update your retrofit builder to use RxJava2CallAdapterFactory -- 并更新您的改造构建器以使用RxJava2CallAdapterFactory -

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

When 2.1.1 is released, you can go back to the regular dependencies. 当2.1.1发布时,您可以返回常规依赖项。

Here is my build.gradle setting, perhaps this would help others 这是我的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'}

If you do not want to update the retrofit version. 如果您不想更新改装版本。 Here is one very good library to convert to and from RxJava1.x to RxJava2.x objects. 是一个非常好的库,可以转换为RxJava1.xRxJava2.x对象。 First, add this 首先,添加这个
compile "com.github.akarnokd:rxjava2-interop:0.10.2"
to build.gradle and Use method build.gradle和Use方法
RxJavaInterop.toV2Observable(observableRx1)
to convert to Rx2 Observables. 转换为Rx2 Observables。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM