订阅者不返回值

问题描述 投票:0回答:1

如何在Kotlin中创建一个Flow.Subscriber?

    val body = this.bodyPublisher().map { p -> {
        val bs  = HttpResponse.BodySubscribers.ofString(StandardCharsets.UTF_8)
        val fs = StringSubscriber(bs)
        p.subscribe(fs)
        bs.body.toCompletableFuture().join()
    } }.get()

它返回一个空字符串

java kotlin java-11
1个回答
2
投票
this.bodyPublisher().map { p -> {
    val bs  = HttpResponse.BodySubscribers.ofString(StandardCharsets.UTF_8)
    val fs = StringSubscriber(bs)
    p.subscribe(fs)
    bs.body.toCompletableFuture().join()
} }

在lambda里面使用大括号会返回一个lambda。它应该更新如下

this.bodyPublisher().map { p -> 
    val bs  = HttpResponse.BodySubscribers.ofString(StandardCharsets.UTF_8)
    val fs = StringSubscriber(bs)
    p.subscribe(fs)
    bs.body.toCompletableFuture().join()
}
© www.soinside.com 2019 - 2024. All rights reserved.