我正在尝试向facebook REST API发出请求,并获得JSON响应。我能够在REST客户端中收集响应,因此我知道我在下面的代码中创建HttpRequest时使用的requestUrl是正确的。但是当我尝试使用akka-http javadsl模仿GET时,我无法理解如何从ResponseEntity中提取json。
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"
}
}
}
答案 0 :(得分:1)
您应该通过调用ResponseEntity entity = response.entity()
并在调用entity.toStrict(timeoutMillis, materialiser).data.decodeString("UTF-8")
之后获取响应实体来获取正文字符串