[英]How to retry the same search query for restaurants to get the next page of results from Google Map Api, using Retrofit and RxJava
I'm doing request to Google Map, to get a list of restaurants, using the Nearby Search Api.我正在向 Google Map 发出请求,以获取餐厅列表,使用附近的搜索 Api。 https://developers.google.com/maps/documentation/places/web-service/search-nearby The server sends its response through pages containing a maximum of 20 results.
https://developers.google.com/maps/documentation/places/web-service/search-nearby服务器通过最多包含 20 个结果的页面发送其响应。 If an extra page is available, the json next_page_token attribute is set, containing the value to be passed as a parameter to the next search.
如果有额外的页面可用,则设置 json next_page_token 属性,其中包含要作为参数传递给下一次搜索的值。 Using retrofit and RxJava, I know how to do the first search, and get the next_page_token.
使用 retrofit 和 RxJava,我知道如何进行第一次搜索,并获取 next_page_token。 But I don't know how to retry the same request.
但我不知道如何重试相同的请求。 Any help will be appreciated.
任何帮助将不胜感激。
public Observable<PlaceNearbySearch> streamFetchRestaurants(String location, int radius, String type) {
// search for the list of restaurants
RestoApi restoApi = RetrofitBuilder.getRetrofit().create(RestoApi.class);
return restoApi.getRestoFirsttPage(location, radius, type, GOOGLE_MAP_API_KEY)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.timeout(10, TimeUnit.SECONDS);
}
public Observable<PlaceDetail> streamFetchDetails(String placeId) {
// search details for each restaurants of the list
RestoApi restoApi = RetrofitBuilder.getRetrofit().create(RestoApi.class);
return restoApi.getPlaceDetails(placeId, GOOGLE_MAP_API_KEY)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.timeout(10, TimeUnit.SECONDS);
}
public Single<List<PlaceDetail>> streamFetchRestaurantDetails(String location, int radius, String type) {
// chains the 2 previous requests
return streamFetchRestaurants(location, radius, type)
.flatMapIterable(new Function<PlaceNearbySearch, List<PlaceNearbySearchPlace>>() {
@Override
public List<PlaceNearbySearchPlace> apply(PlaceNearbySearch placeNearbySearch) throws Exception {
// Is next_page_token set?
// In case it is, how would you query the next page??
Log.d(TAG, "apply: next_page_token: " + placeNearbySearch.getNext_page_token());
return placeNearbySearch.getResultSearches();
}
})
.flatMap(new Function<PlaceNearbySearchPlace, Observable<PlaceDetail>>() {
@Override
public Observable<PlaceDetail> apply(PlaceNearbySearchPlace placeNearbySearchPlace) throws Exception {
return streamFetchDetails(placeNearbySearchPlace.getPlaceId());
}
})
.toList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
private void test_streamFetchRestaurantDetails() throws InterruptedException {
// test method
String location = "48.854685336064264, 2.3467574997969596";
int radius = 1500;
String type = "restaurant";
callstreamFetchRestaurantDetails(location, radius, type);
}
private void callstreamFetchRestaurantDetails(String location, int radius, String type) {
streamFetchRestaurantDetails(location, radius, type)
.subscribe(new DisposableSingleObserver<List<PlaceDetail>>() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onSuccess(@io.reactivex.rxjava3.annotations.NonNull List<PlaceDetail> placeDetails) {
// handle list of placeDetails
}
@Override
public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
Log.d(TAG, "onError: " + e.getMessage());
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.