当我尝试创建 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<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) {
}
});
4 回答
RxJavaCallAdapter
返回RxJava 1Observable
. 您应该将RxJava2CallAdapter用于RxJava2 . 看起来这还没有在官方的改装版本中,但是在2.1.1快照中 . 您可以自己编译适配器,也可以从sonatype snapshot repo中删除依赖项 .将以下内容添加到
build.gradle
的repositories
部分 -将您的改造依赖项更新为
2.1.1-SNAPSHOT
版本 . 请注意,我们也将adapter-rxjava
更改为adapter-rxjava2
-并更新您的改造生成器以使用
RxJava2CallAdapterFactory
-当2.1.1发布时,您可以返回常规依赖项 .
适配器具有版本
2.*.*
这一事实并不意味着它适用于RxJava 2您应该将官方适配器用于第二版RxJava:
然后你可以添加工厂:
这是full answer .
如果您不想更新改装版本 . Here是一个非常好的库,可以转换为
RxJava1.x
到RxJava2.x
对象 . 首先,添加这个compile "com.github.akarnokd:rxjava2-interop:0.10.2"
到
build.gradle
和使用方法RxJavaInterop.toV2Observable(observableRx1)
转换为Rx2 Observables .
这是我的build.gradle设置,也许这会对其他人有所帮助