kotlin stream.assequence()现在都需要stream.iterator()。
我正在使用java.net.httpclient在响应类型httpresponse< stream>中发出简单的获取请求。但是,即使Kotlin.Streams具有 sequence()方法,我现在必须先在调用assequence()之前先将流转换为迭代器。为什么这样?
val client = HttpClient.newHttpClient()
val request = HttpRequest.newBuilder().let { builder ->
builder.uri(URI("http://localhost:8000/response.txt"))
builder.setHeader("Content-Type", "application/json")
builder.GET()
builder.build()
}
val resp: HttpResponse<Stream<String>> = client.send(request, HttpResponse.BodyHandlers.ofLines())
//used to be resp.body().asSequence().map{
resp.body().iterator().asSequence().map {
println("${it.toString()}")
}
I am using java.net.HttpClient to issue a simple GET request where the response is of type HttpResponse<Stream>. However, even though Kotlin.streams has an asSequence() method, I now have to first convert the stream to an iterator before calling asSequence(). Why is this the case?
val client = HttpClient.newHttpClient()
val request = HttpRequest.newBuilder().let { builder ->
builder.uri(URI("http://localhost:8000/response.txt"))
builder.setHeader("Content-Type", "application/json")
builder.GET()
builder.build()
}
val resp: HttpResponse<Stream<String>> = client.send(request, HttpResponse.BodyHandlers.ofLines())
//used to be resp.body().asSequence().map{
resp.body().iterator().asSequence().map {
println("${it.toString()}")
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在build.gradle.kts中,我引用了错误的JDK版本。当我导入
org.jetbrains.kotlin:kotlin-stdlib-jdk8
作为依赖项时,问题解决了。In the build.gradle.kts I was referencing the wrong version of the jdk. The problem resolved when I imported
org.jetbrains.kotlin:kotlin-stdlib-jdk8
as a dependency.