2016-12-24 82 views
1

當試圖創建和訂閱Subscriber時,我收到以下錯誤消息。Retrofit2 + RxJava2 + RxAndroid錯誤

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

      } 
    }); 

回答

6
RxJavaCallAdapter

返回RxJava 1 Observable。對於RxJava2,您應該使用RxJava2CallAdapter。看起來這還不是正式的改造版本,但是在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發佈,你可以回去到正規的依賴。

2

該適配器具有版本2.*.*並不意味着它是爲了用於RxJava 2事實

你應該使用官方的適配器爲版本RxJava的:

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

這是full answer

1

如果您不想更新改造版本。 Here是一個非常好的庫,可以將其從RxJava1.x轉換爲RxJava2.x對象。 首先,添加此
compile "com.github.akarnokd:rxjava2-interop:0.10.2"
build.gradle和使用方法
RxJavaInterop.toV2Observable(observableRx1)
轉換爲Rx2的觀測量。

0

這是我的構建。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'} 
相關問題