-1
我HAVA寫了阿卡HTTP客戶端的例子,但我不能讀的HttpResponse體爲字符串,我的代碼如下:如何閱讀整個的HttpResponse體與阿卡流HTTP字符串
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)
}
}
}
}
和其結果是:
none值
誰能告訴我爲什麼?以及如何編寫代碼? 非常感謝