rxjava .zip onNext()未执行

时间:2017-04-17 11:06:40

标签: android rx-android rx-java2 reactivex

我在zip中组合了多个观察者来执行来自服务器的多个请求但是onNext没有执行,因此我无法获得返回的值。 menuName,restaurantName和userName是全局变量。

public void getOrderValues(String restaurantIdx,String menuIdx,String userIdx ) {

    try {
        Observable<Restaurant> orderRestaurant = IdentityClient.getAPIService()

                .getRestaurantById(restaurantIdx)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread());


        Observable<Menu> orderMenu = IdentityClient.getAPIService()
                .getMenuById(menuIdx)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread());


        Observable<User> orderUser = IdentityClient.getAPIService()
                .getUserById(userIdx)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread());


        Observable.zip(orderRestaurant, orderMenu, orderUser, new Function3<Restaurant, Menu, User, OrderValues>() {
            @Override
            public OrderValues apply(@NonNull Restaurant restaurant,@NonNull Menu menu,@NonNull User user) throws Exception {
                return new OrderValues(restaurant, menu, user);
            }
        }).subscribe(new Observer<OrderValues>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.e("Eatright Error", d.toString()+"");
            }

            @Override
            public void onNext(OrderValues orderValues) {
                menuName = orderValues.menu.getName();
                restaurantName = orderValues.restaurant.getName();
                userName = orderValues.user.getUserName();

            }

            @Override
            public void onError(Throwable e) {
                Log.e("Eatright Error", e.getMessage()+"");
            }

            @Override
            public void onComplete() {
                Log.e("Eatright Error","Completed");
            }
        });
    } catch (Exception e) {
        Log.e("Error Eatright", e.getMessage()+"");
    }


}}
  //GET restaurant by Id
@GET("/api/restaurants/getrestaurant/{id}")
Observable<Restaurant> getRestaurantById(
        @Path("id") String id);

//GET menu by Id
@GET("/api/menus/getmenu/{id}")
Observable<Menu> getMenuById(
        @Path("id") String id);

 @GET("/api/account/user/{id}")
Observable<User> getUserById( @Path("id") String id
);

当我将观察者订阅给个别观察者时,我在onNext上获得了返回值,但在我将它们组合时却没有。

对邮递员进行api测试

    {
  "RestaurantId": "00000000-0000-0000-0000-000000000000",
  "Name": "Papaye",
  "City": null,
  "Town": null,
  "Description": null,
  "Image": null,
  "DateCreated": null,
  "Contact1": null,
  "Contact2": null,
  "NumOfSeats": null
}

{
  "MenuId": "00000000-0000-0000-0000-000000000000",
  "RestaurantId": null,
  "Name": "Fufu and Light Soup",
  "Price": null,
  "MainImage": null,
  "LastModified": null,
  "DateCreated": null,
  "Type": null
}

{
  "userName": "buelersandra@gmail.com"
}

2 个答案:

答案 0 :(得分:1)

试试这个:

Observable.zip(orderRestaurant, orderMenu, orderUser, new Function3<Restaurant, Menu, User, OrderValues>() {
            @Override
            public OrderValues apply(@NonNull Restaurant restaurant,@NonNull Menu menu,@NonNull User user) throws Exception {
                return new OrderValues(restaurant, menu, user);
            }
        }).subscribe(new Consumer<Object>() {
            @Override
            public void accept(@NonNull Object o) throws Exception {
                menuName = orderValues.menu.getName();
                restaurantName = orderValues.restaurant.getName();
                userName = orderValues.user.getUserName();
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable e) throws Exception {
                Log.e("Eatright Error", e.getMessage()+"");
            }
        }, new Action() {
            @Override
            public void run() throws Exception {
                Log.e("Eatright Error","Completed");
            }
        });

答案 1 :(得分:0)

尝试改变你的代码:

public void getOrderValues(String restaurantIdx,String menuIdx,String userIdx ) {

try {
    Observable<Restaurant> orderRestaurant = IdentityClient.getAPIService()

            .getRestaurantById(restaurantIdx)
            .subscribeOn(Schedulers.newThread());

    Observable<Menu> orderMenu = IdentityClient.getAPIService()
            .getMenuById(menuIdx)
            .subscribeOn(Schedulers.newThread());

    Observable<User> orderUser = IdentityClient.getAPIService()
            .getUserById(userIdx)
            .subscribeOn(Schedulers.newThread());

    Observable.zip(orderRestaurant, orderMenu, orderUser, new Function3<Restaurant, Menu, User, OrderValues>() {
        @Override
        public OrderValues apply(@NonNull Restaurant restaurant,@NonNull Menu menu,@NonNull User user) throws Exception {
            return new OrderValues(restaurant, menu, user);
        }
    })
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Observer<OrderValues>() {
        @Override
        public void onSubscribe(Disposable d) {
            Log.e("Eatright Error", d.toString()+"");
        }

        @Override
        public void onNext(OrderValues orderValues) {
            menuName = orderValues.menu.getName();
            restaurantName = orderValues.restaurant.getName();
            userName = orderValues.user.getUserName();

        }

        @Override
        public void onError(Throwable e) {
            Log.e("Eatright Error", e.getMessage()+"");
        }

        @Override
        public void onComplete() {
            Log.e("Eatright Error","Completed");
        }
    });
} catch (Exception e) {
    Log.e("Error Eatright", e.getMessage()+"");
}


}}