I hava written a akka http client example,but i can not read HttpResponse body as a String,my code is below:
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)
}
}
}
}
and the result is :
none value
who can tell me why? and how to write the code? thanks very much