![](/img/trans.png)
[英]Android: How to search for nearby hotels using the Google Places API (Autocomplete)?
[英]How to combine Places nearby api and Places details api? retrofit java rx
我對android很新,我需要一些幫助。 我需要在用戶附近找到餐館,並在回收者視圖中顯示他們的信息。 所以我使用Places api:附近和細節。 我有兩個請求,一個請求獲取用戶附近的一個餐館列表(我檢索一個包含餐館對象列表的對象,我將它們保存在一系列餐館對象中)以及一個獲取每個餐館的詳細信息。第一個請求(它需要使用的地方s id,第一個請求找到)。 問題是請求沒有以正確的順序執行,因此我的回收站視圖不會以良好的方式顯示。 我應該把我的請求鏈接到只有一個? 我做了一些研究,但我找不到怎么做,因為我需要在第一個請求中找到每個餐館的第二個請求。
這是我的流
我應該怎么用它們制作一個流?
private static PlacesService placesService = PlacesService.retrofit.create(PlacesService.class);
public static Observable<RestaurantObject> streamFetchRestaurants(String latitudeLongitude, int radius, String type, String apiKey) {
return placesService.getRestaurants(latitudeLongitude,radius,type, apiKey)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.timeout(10, TimeUnit.SECONDS);
}
public static Observable<RestaurantInformationObject> streamFetchRestaurantInfos(String id, String apikey) {
return placesService.getRestaurantInfo(id, apikey)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.timeout(10, TimeUnit.SECONDS);
}
使用concatMap
應該存檔你想要的東西:
streamFetchRestaurants(latLon, radius, type, apiKey)
.concatMap(restaurantObject -> streamFetchRestaurantInfos(restaurantObject.id, apiKey))
.subscribe(restaurantInformationObject -> {
// Do something with the restaurant information object
});
與flatMap
相比,它保留了餐廳物品的原始順序。 有關更多詳細信息, 請查看RxJava或以下文章的文檔: https ://fernandocejas.com/2015/01/11/rxjava-observable-tranformation-concatmap-vs-flatmap/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.