Scala:Http 客户端读取 http 响应

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

我正在使用

httpClient
lib 来使用 scala 进行一些
REST API
调用。我可以发布数据。

我正在使用以下代码来阅读内容。但是,当我在 Spark Databricks Cluster 上运行时,它给了我一个错误。

 val entity = response.getEntity
    var content = ""
    if (entity != null) {
      val inputStream = entity.getContent
      content = io.Source.fromInputStream(inputStream).getLines.mkString
      inputStream.close()
    }

错误

error: object Source 不是 package io 的成员 内容 = io.Source.fromInputStream(inputStream).getLines.mkString

有什么方法可以修复这个错误,或者用不同的方法来读取 HTTP 响应内容。

scala apache-httpclient-4.x
1个回答
1
投票

请尝试

import scala.io.Source

或者像这样使用完整的包名称:

content = scala.io.Source.fromInputStream(inputStream).getLines.mkString
© www.soinside.com 2019 - 2024. All rights reserved.