最近我使用API深入研究了一些工作.API使用Unirest http库来简化从Web接收的工作.当然,由于从API服务器调用数据,我试图通过使用API的异步调用来提高效率.我的想法结构如下:
通过返回期货结果来创建数据数组
显示数据+从数据中收集的其他信息
因此,在开始第二步之前,我需要返回所有数据.我的代码如下:
Future < HttpResponse < JsonNode > > future1 = Unirest.get("https://example.com/api").asJsonAsync(new Callback < JsonNode > () { public void failed(UnirestException e) { System.out.println("The request has failed"); } public void completed(HttpResponse < JsonNode > response) { System.out.println(response.getBody().toString()); responses.put(response); } public void cancelled() { System.out.println("The request has been cancelled"); } }); Future < HttpResponse < JsonNode > > future2 = Unirest.get("https://example.com/api").asJsonAsync(new Callback < JsonNode > () { public void failed(UnirestException e) { System.out.println("The request has failed"); } public void completed(HttpResponse < JsonNode > response) { System.out.println(response.getBody().toString()); responses.put(response); } public void cancelled() { System.out.println("The request has been cancelled"); } }); doStuff(responses);
我怎样才能这样做,只有在两个期货完成后才会调用doStuff?