【问题标题】:wait for multiple volley responses in a for loop在 for 循环中等待多个凌空响应
【发布时间】:2016-05-15 11:57:46
【问题描述】:

我正在使用 volley singleton 并将所有 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 循环,它将执行多个 volley 请求。

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

但是为了显示我的新活动,我需要之前完成 for 循环中所有凌空请求的数据,它会离开此活动并进入新活动。

【问题讨论】:

    标签: android-volley


    【解决方案1】:

    我的方法基本上是设置 2 个 int 变量:我用来监控 volley 请求的 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();
    
    
    
    
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2021-11-04
      • 1970-01-01
      • 2023-01-20
      • 2018-08-26
      • 2019-07-18
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多