Android 多个连续观测值,每个观测值之间有时间延迟
我正在尝试获取Android 多个连续观测值,每个观测值之间有时间延迟,android,google-maps,google-places-api,rx-java,rx-android,Android,Google Maps,Google Places Api,Rx Java,Rx Android,我正在尝试获取googleplaces,根据api限制,每个请求不能获得超过20个位置,下一个请求需要上一个请求的nextPageToken 问题: LatLng some = new LatLng(31.471199, 74.234940); Observable .zip(getNearByPlaces(some), getNearByPlaces(some), getNearByPlaces(some), new Func3<List<PlaceInfo>
googleplaces
,根据api限制,每个请求不能获得超过20个位置,下一个请求需要上一个请求的nextPageToken
问题:
LatLng some = new LatLng(31.471199, 74.234940);
Observable
.zip(getNearByPlaces(some), getNearByPlaces(some), getNearByPlaces(some), new Func3<List<PlaceInfo>, List<PlaceInfo>, List<PlaceInfo>, List<PlaceInfo>>() {
@Override
public List<PlaceInfo> call(List<PlaceInfo> placeInfos, List<PlaceInfo> placeInfos2, List<PlaceInfo> placeInfos3) {
placeInfos2.addAll(placeInfos3);
placeInfos.addAll(placeInfos2);
return placeInfos;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<PlaceInfo>>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(List<PlaceInfo> placeInfos) {
adapter.setPlaces(placeInfos);
}
});
操作员立即执行所有请求,但我需要在每次Zip
呼叫之间,将它们连续、及时地延迟可观察到的
4秒
运算符允许等待,但实际情况是,它会立即执行Delay
调用,并在给定的延迟时间后返回结果,这是不需要的Zip
LatLng some = new LatLng(31.471199, 74.234940);
Observable
.zip(getNearByPlaces(some), getNearByPlaces(some), getNearByPlaces(some), new Func3<List<PlaceInfo>, List<PlaceInfo>, List<PlaceInfo>, List<PlaceInfo>>() {
@Override
public List<PlaceInfo> call(List<PlaceInfo> placeInfos, List<PlaceInfo> placeInfos2, List<PlaceInfo> placeInfos3) {
placeInfos2.addAll(placeInfos3);
placeInfos.addAll(placeInfos2);
return placeInfos;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<PlaceInfo>>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(List<PlaceInfo> placeInfos) {
adapter.setPlaces(placeInfos);
}
});
LatLng some=新的LatLng(31.471199,74.234940);
可观察
.zip(getNearByPlaces(部分)、getNearByPlaces(部分)、getNearByPlaces(部分)、new Func3(){
@凌驾
公共列表调用(列表placeInfos、列表placeInfos2、列表placeInfos3){
placeInfos2.addAll(placeInfos3);
placeInfos.addAll(placeInfos2);
返回地点信息;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.订阅(新观察员){
@凌驾
未完成的公共无效(){
}
@凌驾
公共无效申报人(可丢弃的e){
e、 printStackTrace();
}
@凌驾
public void onNext(列表位置信息){
adapter.setPlaces(placeInfos);
}
});
我是初学者,大约一周前开始使用rxjava,所以您可能会遇到错误。假设您有以下响应模型
class Response {
private String pageToken;
private List<PlaceInfo> places;
//setters, getters etc
}
这样你就可以做你需要的事情了
getNearByPlaces(latLng)
.compose(addNextPlaces())
//add here additional compose methods if you need them
.subscribe(resultResponse -> {
resultResponse.getPlaces() //handle result List
}, error -> {
//handle error
});
注意:注意穿线<代码>延迟将调度程序切换到计算
p.p.S.我怀疑有一个更简单、更优雅的解决方案:)谢谢,它起作用了,最后一个问题是它是一个三个请求的过程,但上面的示例是处理两个请求,我想
。compose(addNextPlaces())
添加了一个额外的请求,因此,如果您想链接更多请求,只需复制粘贴此字符串并将其放在下面
getNearByPlaces(latLng)
.compose(addNextPlaces())
//add here additional compose methods if you need them
.subscribe(resultResponse -> {
resultResponse.getPlaces() //handle result List
}, error -> {
//handle error
});