Pri*_*tel 8 java android rx-android retrofit2 rx-java2
我在尝试打包Subscriber和订阅时收到如下错误消息.
can not resolve method 'subscribe(anonymous rx.Subscriber<GooglePlacesResponse>)'
Run Code Online (Sandbox Code Playgroud)
的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
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
可观察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) {
}
});
Run Code Online (Sandbox Code Playgroud)
Gak*_*ket 13
适配器具有版本的2.*.*事实并不意味着它适用于RxJava 2
您应该将官方适配器用于第二版RxJava:
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' // works with RxJava 2
Run Code Online (Sandbox Code Playgroud)
然后你可以添加工厂:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.example.com")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
Run Code Online (Sandbox Code Playgroud)
这是完整的答案.
iag*_*een 11
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/"
}
}
Run Code Online (Sandbox Code Playgroud)
更新您对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'
Run Code Online (Sandbox Code Playgroud)
并更新您的改造生成器以使用RxJava2CallAdapterFactory-
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(baseUrl)
.build();
Run Code Online (Sandbox Code Playgroud)
当2.1.1发布时,您可以返回常规依赖项.
| 归档时间: |
|
| 查看次数: |
7462 次 |
| 最近记录: |