问题
I am attempting to use Scala dispatch but been both a Scala neewbie and the face that Dispatch api is symbol crazy Im confused on how I can stream a large http response and process it line by line. Any help would be appreciated.
Cheers, Chris.
Note:
This isn't working for me:
Http(url(Config.publisherUrl) > as.stream.Lines(line => println(line)))
The lines are never printed.
Edit:
The lines were being printed, but only when there was more than one line in the response. The issue seems to be that I can stream the data line by line, but the very last line is omitted.
回答1:
I had the same problem. What you could do. I read it as input stream, then converted it to Akka stream and returned source.
import akka.stream.scaladsl.{Source, StreamConverters}
val futureStream = Http(url(urlString) > as.Response(_.getResponseBodyAsStream))
futureStream.map { inputStream =>
val source = () => inputStream
StreamConverters.fromInputStream(source)
}
It worked for me.
来源:https://stackoverflow.com/questions/17606186/scala-dispatch-stream-response-line-by-line