如何使用Akka Streams HTTP将整个HttpResponse正文作为字符串读取

如何使用Akka Streams HTTP将整个HttpResponse正文作为字符串读取,akka,akka-http,Akka,Akka Http,我已经编写了一个akka http客户端示例,但我无法将HttpResponse主体作为字符串读取,我的代码如下: import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpResponse, HttpRequest} import akka.stream.ActorMaterializer import scala.concurrent.duration._

我已经编写了一个akka http客户端示例,但我无法将HttpResponse主体作为字符串读取,我的代码如下:

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpResponse, HttpRequest}
import akka.stream.ActorMaterializer
import scala.concurrent.duration._

import scala.util.{Failure, Success}


object TestHttp {
  def main(args: Array[String]) {
    implicit val system = ActorSystem()
    implicit val materializer = ActorMaterializer()
    implicit val executionContext = system.dispatcher

    val url = "http://www.baidu.com"
    println(url)
    val responseFuture = Http().singleRequest(HttpRequest(uri = url))
    responseFuture.andThen {
      case Success(resp: HttpResponse) => {
        //println(resp.status.intValue())
        //println(resp.status.defaultMessage())
        //val aaaa = resp.entity.dataBytes.runFold(ByteString(""))(_ ++ _)
        //println(aaaa.value.get.get.decodeString("UTF-8"))
        //println(resp.entity.dataBytes.via(Framing.delimiter(ByteString("\n"),maximumFrameLength = 256,allowTruncation = true)).map(_.utf8String))
      val entity = resp.entity.toStrict(5 seconds).map(_.data.decodeString("UTF-8"))
      println(entity.value.getOrElse("none value"))
      //nodeCount=JsonUtil.nodeCount(entity.value.get.get)
    }
    case Failure(ex:Exception) => {
      println("http request error:"+ex.getMessage)
    }
  }
 }
}
结果是:

无值

谁能告诉我为什么?如何编写代码?
非常感谢

请立即在一个位置提问,这个问题在被问完之后已经很快被问到了

请注意:

如果未来未完成,则返回的值将为“无”。如果未来已完成,则如果包含有效结果,则该值将为Some(成功(t)),如果包含异常,则该值将为Some(失败(错误))

所以你只是在它有机会完成之前展望未来,所以它是空的

相反,使用诸如
map
onComplete
之类的操作来检查它