在for循环中等待多个齐射响应

时间:2016-05-15 11:57:46

标签: android-volley

我正在使用排球单打并将所有排球请求添加到其中。

将排球请求添加到队列

的示例代码
MyApplication.getInstance().addToReqQueue(jsObjRequest, "jreq1");

我有一个onclick功能。

buttonId.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    for(int i=0;i<4;i++){
                     //....... here i call for asycn volley requests which get added to the queue of volleysingleton

                    }

                    // ******how to ensure all my volley requests are completed before i move to next step here.*****


                    //calling for new intent
                    Intent m = new Intent(PlaceActivity.this, Myplanshow.class);
                        m.putExtra("table_name", myplansLists.get(myplansLists.size() - 1).table_name);
                        m.putExtra("table_name_without_plan_number", myplansLists.get(myplansLists.size() - 1).place_url_name);
                        m.putExtra("changed", "no");
                        m.putExtra("plannumber", myplansLists.size());

                    //moving to new intent;
                        v.getContext().startActivity(m);

}
            });

在onclick里面我有一个for循环,它将执行多个凌空请求。

在for循环之后,它将通过意图开始一个新的活动。

但是对于我要展示的新活动,我需要先完成for循环中所有排球请求的数据,它会离开此活动并进入新活动。

1 个答案:

答案 0 :(得分:0)

我的方法基本上是设置2个int变量:successCount和errorCount,用于监视截击请求。在每个请求的onResponse中,我递增successCount变量,然后在onErrorResponse中,我增加errorCount。最后,我检查两个变量的总和是否等于发出的请求数,如果不是,则线程在循环中等待。 检查一下:

 buttonId.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {


        new Runnable(){
            @Override
            public void run() {
                int successCount=0;
                int errorCount=0;
                for(int i=0;i<4;i++){
                    //....... here i call for asycn volley requests which get added to the queue of volleysingleton
                    //in the onResponse of each of the volley requests, increment successCount by 1;
                    // i.e successCount++;
                    //also in onErrorResponse of each of the volley requests, increment
                    // errorCount by 1

                }

                // ******how to ensure all my volley requests are completed before i move to next step here.*****

                // wait here till all requests are finished
                while (successCount+errorCount<4)
                {
                    Log.d("Volley"," waiting");

                }

                //calling for new intent
                Intent m = new Intent(PlaceActivity.this, Myplanshow.class);
                m.putExtra("table_name", myplansLists.get(myplansLists.size() - 1).table_name);
                m.putExtra("table_name_without_plan_number", myplansLists.get(myplansLists.size() - 1).place_url_name);
                m.putExtra("changed", "no");
                m.putExtra("plannumber", myplansLists.size());

                //moving to new intent;
                v.getContext().startActivity(m);
            }
        }.run();




    }
});