Android在X秒后运行方法X次

我想根据我的购物车商品数量执行HTTP改装发布方法,并在几秒钟后执行此调用(以便成功执行先前的发布调用)。

这是我在做什么。

private void submitOrderItems(String id_order) {

    final android.app.AlertDialog waitingDialog = new SpotsDialog(CheckOutActivity.this);
    waitingDialog.show();
    waitingDialog.setMessage("Placing ...");


    int count = new Database(getApplicationContext()).getCarts().size();
    for (int i = 0; i < count; i++) {

        int finalI = i;
        handler.postDelayed(runnable = new Runnable() {
            @Override
            public void run() {
                double food_total_price = 0.00;
                food_total_price += (Double.parseDouble(cartCart.get(finalI).getPrice())) * (Double.parseDouble(cartCart.get(finalI).getQuantity()));

                ApiService service = ApiClient.getClientVegetables().create(ApiService.class);
                //defining the call
                Call<Result> call = service.placeOrderItems(
                        id_order,
                        cartCart.get(finalI).getFood_id(),
                        cartCart.get(finalI).getName(),
                        cartCart.get(finalI).getPrice(),
                        cartCart.get(finalI).getQuantity(),
                        String.valueOf(food_total_price),
                        cartCart.get(finalI).getImage_url(),
                        cartCart.get(finalI).getMin_unit_amount(),
                        cartCart.get(finalI).getUnit(),
                        cartCart.get(finalI).getId_menu(),
                        cartCart.get(finalI).getMenu_name()
                );
                //calling the api
                call.enqueue(new Callback<Result>() {
                    @Override
                    public void onResponse(Call<Result> call, Response<Result> response) {
                        //waitingDialog.dismiss();
                    }

                    @Override
                    public void onFailure(Call<Result> call, Throwable t) {
                        //waitingDialog.dismiss();
                        Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });

                handler.postDelayed(runnable, 2000);
            }
        }, 2000);
    }
    waitingDialog.dismiss();
    //sentNotificationToServer(id_order);
    startActivity(new Intent(CheckOutActivity.this, ConfirmationActivity.class));
    finish();
}

但是这里的问题是,并非所有购物车项目数据都已提交。例如:如果购物车中有100件物品,那么将提交40-50件物品数据。如果项目低于40(并非每次都低于),则将提交所有项目。可能是什么原因?对于服务器端,我正在使用Slim框架。

评论