2016-08-13 53 views
2

我正在嘗試向facebook REST API發出請求,並返回JSON響應。我可以在REST客戶端收集響應,因此我知道在下面的代碼中創建HttpRequest時使用的requestUrl是正確的。但是當我嘗試使用akka-http javadsl來模擬GET時,我無法理解如何從ResponseEntity中提取json。從akka http javadsl提取響應json字符串ResponseEntity

final HttpRequest request = HttpRequest.GET(requestUrl); 
final Materializer materializer = ActorMaterializer.create(this.context.getActorSystem()); 

final CompletionStage<HttpResponse> responseFuture = 
       Http.get(this.context.getActorSystem()).singleRequest(request, materializer); 

final HttpResponse response = responseFuture.toCompletableFuture().get(); 

我期待的迴應的東西如下 -

{ 
    "data": [ 
     { 
     "cpc": 9.7938056680162, 
     "clicks": "247", 
     "impressions": "15949", 
     "spend": 2419.07, 
     "date_start": "2016-06-15", 
     "date_stop": "2016-08-13" 
     } 
    ], 
    "paging": { 
     "cursors": { 
     "before": "MAZDZD", 
     "after": "MAZDZD" 
     } 
    } 
} 

回答

1

你應該通過調用ResponseEntity entity = response.entity()從響應得到響應實體和呼叫entity.toStrict(timeoutMillis, materialiser).data.decodeString("UTF-8")後能得到身體的字符串

您可以查找這些方法的簽名official API documentation