排球同步请求

时间:2013-12-18 16:46:48

标签: java android future android-volley

我需要发出五个请求并仅在完成所有5个请求时解析结果,以传统方式实现和意图服务使用来自java.net或类似库的HttpURLConnection执行五个请求并解析结果。然而,我正在试验Volley,我需要同步请求,我想这样做:

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
  }
}

这是要走的路吗?或者还有其他选择,那里没有太多的Volley文档。

0 个答案:

没有答案
相关问题