Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit2 + RxJava2 + RxAndroid error

I am getting error message as below when try to crate Subscriber and subscribe.

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> is as below.

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) {

            }
     });
like image 304
Priyank Patel Avatar asked Dec 24 '16 10:12

Priyank Patel


People also ask

What is RxAndroid?

RxAndroid: Reactive Extensions for AndroidThis module adds the minimum classes to RxJava that make writing reactive components in Android applications easy and hassle-free. More specifically, it provides a Scheduler that schedules on the main thread or any given Looper .

Is RxJava dying?

RxJava, once the hottest framework in Android development, is dying. It's dying quietly, without drawing much attention to itself.

What is ReactiveX Android?

ReactiveX, also known as Reactive Extensions or RX, is a library for composing asynchronous and event-based programs by using observable sequences. This is perfect for Android, which is an event-driven and user-focused platform.


3 Answers

The fact that adapter has version 2.*.* does not mean that it is intended for use with RxJava 2

You should use the official adapter for the second version of 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.

like image 186
Gaket Avatar answered Oct 19 '22 04:10

Gaket


RxJavaCallAdapter returns a RxJava 1 Observable. You should use RxJava2CallAdapter for RxJava2. Looks like that is not in an official retrofit release yet, but is in the 2.1.1 snapshot. You can either compile the adapter yourself, or pull the dependencies off the sonatype snapshot repo.

Add the following to your repositories section in your build.gradle --

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

Update your retrofit dependencies to the 2.1.1-SNAPSHOT version. Note that we also change adapter-rxjava to 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 --

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.

like image 33
iagreen Avatar answered Oct 19 '22 05:10

iagreen


Here is my build.gradle setting, perhaps this would help others

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'}
like image 35
llb Avatar answered Oct 19 '22 06:10

llb