2016-05-15 5 views
0

Ich benutze Volley Singleton und fügen Sie alle Volley-Anfrage zu ihm.warten auf mehrere Volley-Antworten in einer for-Schleife

Beispielcode des Addieren Volley Anfrage in der Warteschlange

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

I eine OnClick Funktion habe.

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

} 
      }); 

Inside onclick Ich habe eine for-Schleife, die mehrere Volley-Anfragen ausführen wird.

Nach der for-Schleife wird eine neue Aktivität mit Absicht gestartet.

Aber für meine neue Aktivität zu zeigen, ich brauche die Daten aller Volley-Anfragen in der FOR-Schleife vor abgeschlossen werden, verlässt es diese Aktivität und geht zu neuen Aktivitäten.

Antwort

0

Meine Vorgehensweise besteht im Wesentlichen darin, 2 int-Variablen einzurichten: successCount und errorCount, die ich zur Überwachung der Volley-Anfragen verwende. In der onResponse jeder Anfrage habe ich die Variable successCount inkrementiert, dann habe ich in der onErrorResponse den errorCount inkrementiert. Am Ende überprüfe ich, ob die Summe beider Variablen gleich der Anzahl der Anfragen ist, wenn nicht, wartet der Thread in einer Schleife. überprüfen Sie dies:

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

Ich werde versuchen, Aussehen sollte funktionieren. –