我需要发出五个请求并仅在完成所有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文档。