2016-12-24 31 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<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のスナップショットにあります。アダプターを自分でコンパイルするか、ソーナー・タイプのスナップショット・リポジトリーから依存関係を取り出すことができます。

あなたbuild.gradleであなたのrepositoriesセクションに以下を追加 - 2.1.1-SNAPSHOTバージョンに換装の依存関係を更新し

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

を。 2.1.1がリリースされたとき、あなたは定期的な依存関係に戻ることができ

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

からRxJava2CallAdapterFactoryを使用するように改造ビルダーを

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' 

と更新 - 私たちはまたadapter-rxjavaadapter-rxjava2に変更していることに注意してください。

2

アダプターは、それがRxJava 2での使用を目的としていることを意味するものではありませんバージョン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に変換するライブラリとして非常に優れています。 まず、Rx2の観測に変換し、この
compile "com.github.akarnokd:rxjava2-interop:0.10.2"

RxJavaInterop.toV2Observable(observableRx1)
build.gradleにと使用方法を追加します。

0

ここは私のビルドです。これはおそらく他人を助けるでしょう

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'} 
関連する問題