如何将附近API的位置相结合,并将细节API放置?改造Java RX



我是Android的新手,我需要一些帮助。我需要在用户附近获取餐厅,并在回收器视图中显示其信息。因此,我使用位置API:附近的详细信息。我有两个请求,一个请求在用户附近获取一个餐厅清单(我检索了一个包含餐馆对象列表的对象,我将它们保存在一系列餐馆对象中(一个可以获取第一个请求找到的每个餐厅的详细信息(它需要在第一个请求中找到的地点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-brobservable-tranformation-concatmap-vs-vs-flatmap/

最新更新