我在尝试创建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) {
}
});
答案 0 :(得分:11)
适配器具有版本<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<div class="myDiv">
<img src="http://www.slidesjs.com/img/example-slide-350-2.jpg" data-tablet="http://www.w3schools.com/w3css/img_mountains_wide.jpg" data-mobil="http://wowslider.com/sliders/demo-23/data1/images/landscape1344620.jpg">
<img src="http://www.w3schools.com/w3css/img_fjords_wide.jpg" data-tablet="http://semtasoft.com/wp-content/gallery/semtasoft/Slide-img-4.jpg" data-mobil="http://2.bp.blogspot.com/-0xYI1ZJjncM/U0eM2WjhLBI/AAAAAAAAIRk/ytNDG8Nf1x4/s1600/slide-img-1.jpg">
<img src="http://hdimagesnew.com/wp-content/uploads/2015/11/New-Wallpapers-HD.jpg" data-mobil="http://2.bp.blogspot.com/-0xYI1ZJjncM/U0eM2WjhLBI/AAAAAAAAIRk/ytNDG8Nf1x4/s1600/slide-img-1.jpg">
<img src="http://csgowallpapers.com/assets/images/original/mossawi_697490225546_20161227125412_701766109559.png" data-tablet="http://2.bp.blogspot.com/-0xYI1ZJjncM/U0eM2WjhLBI/AAAAAAAAIRk/ytNDG8Nf1x4/s1600/slide-img-1.jpg">
<img src="http://csgowallpapers.com/assets/images/original/mossawi_521575293969_20170108191041_920035389717.png">
</div>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</body>
</html>
的事实并不意味着它适用于RxJava 2
您应该将官方适配器用于第二个版本的RxJava:
2.*.*
然后你可以添加工厂:
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0 // works with RxJava 2
这是full answer。
答案 1 :(得分:10)
RxJavaCallAdapter
返回RxJava 1 Observable
。您应该将RxJava2CallAdapter
用于RxJava2。看起来还没有正式的改版版本,但是在2.1.1快照中。您可以自己编译适配器,也可以从sonatype snapshot repo中删除依赖项。
将以下内容添加到repositories
-
build.gradle
部分
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后,您可以返回常规依赖项。
答案 2 :(得分:2)
这是我的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'}
答案 3 :(得分:1)
如果您不想更新改装版本。 Here是一个非常好的库,可以转换为RxJava1.x
到RxJava2.x
个对象。
首先,添加这个
compile "com.github.akarnokd:rxjava2-interop:0.10.2"
到build.gradle
和使用方法
RxJavaInterop.toV2Observable(observableRx1)
转换为Rx2 Observables。