2016-05-15 75 views
0

我正在使用排球單身並添加所有排球請求。加入凌空請求的等待for循環中的多個排球響應

示例代碼排隊

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循環後,它會通過intent開始一個新的活動。

但是對於我的新活動來說,我需要for循環中的所有抽籤請求的數據在之前完成,它將離開此活動並轉到新活動。

回答

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(); 




    } 
}); 
+0

我會嘗試,看起來應該工作。 –