[英]Retrofit2 + RxJava2 + RxAndroid error
當我嘗試創建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) {
}
});
適配器具有版本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();
這是完整的答案 。
RxJavaCallAdapter
返回一個RxJava 1 Observable
。 您應該將RxJava2CallAdapter
用於RxJava2。 看起來還沒有正式的改版版本,但是在2.1.1快照中。 您可以自己編譯適配器,也可以從sonatype snapshot repo中刪除依賴項。
將以下內容添加到build.gradle
repositories
部分 -
repositories {
// Other repos...
maven {
url = "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
將您的改進依賴項更新為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'
並更新您的改造構建器以使用RxJava2CallAdapterFactory
-
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(baseUrl)
.build();
當2.1.1發布時,您可以返回常規依賴項。
這是我的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'}
如果您不想更新改裝版本。 這是一個非常好的庫,可以轉換為RxJava1.x
和RxJava2.x
對象。 首先,添加這個
compile "com.github.akarnokd:rxjava2-interop:0.10.2"
build.gradle
和Use方法
RxJavaInterop.toV2Observable(observableRx1)
轉換為Rx2 Observables。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.