I’m doing request to Google Map, to get a list of restaurants, using the Nearby Search 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. 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. Using retrofit and RxJava, I know how to do the first search, and get the 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());
}
});
}