1

I need to make five requests and parse the results only when all 5 are completed, in a traditional way i will implement and intent service do the five requests with HttpURLConnection from java.net or similar library and parse the results. However i'm experimenting Volley and i need synchronous requests i'm thinking of doing it like this:

ArrayList<RequestFuture<JSONObject>> futures = new ArrayList<RequestFuture<JSONObject>>();
String url;
int i = 0;   
while(i<5){

  url = //get the url for each request;

  //Create futures
  RequestFuture<JSONObject> future = RequestFuture.newFuture();

  //Add it to arrayList
  futures.add(future);

  //Queue it
  requestQueue.add(new JsonObjectRequest(url, new JSONObject(), future, future));

  i++
}

//Wait for each one to complete
for( RequestFuture future : futures){

  try {

    future.get(); // get the result possibly save each result and then process it

  } catch (InterruptedException e) {
    // exception handling
  } catch (ExecutionException e) {
    // exception handling
  }
}

Is this the way to go ? Or is there any other alternative, there isn't much Volley documentation out there.

Sergio Serra
  • 1,479
  • 3
  • 17
  • 25
  • 2
    Duplicate question: http://stackoverflow.com/questions/16904741/can-i-do-a-synchronous-request-with-volley?rq=1 – Submersed Dec 18 '13 at 16:55

0 Answers0