2016-05-15 7 views
0

私は、ボレーシングルトンを使用しており、すべてのボレーリクエストを追加しています。私はonclickの機能を持っているforループで複数のボレー応答を待つ

MyApplication.getInstance().addToReqQueue(jsObjRequest, "jreq1"); 

をキューにボレー要求を追加する

サンプルコード。

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ループのすべてのボレーリクエストのデータが前に完了する必要があります。このアクティビティは終了し、新しいアクティビティに移動します。

答えて

0

私のアプローチは、2つのint変数を設定することです:successCountとerrorCountは、私がvolley要求を監視するために使用します。各リクエストの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

私はしようとすると、動作する必要があります。 –

関連する問題