json - java.io.IOException:流已关闭
问题描述
当我尝试从 http 连接打印响应时,出现以下错误。我想我两次使用流,因此它说流已关闭但不确定。
错误出现在writeInstFile()
我尝试使用打印响应的方法上getInputStream().getText()
我只是想创建新的字符串变量并将连接流响应放入该变量并打印它,以便我可以看到当我尝试执行 http 请求时得到的响应:
private void writeInstFile(File outFile) {
Properties config = workflowEnvironment.getConfig()
//http connection to get Authentication token
HttpURLConnection connection = connection("https://XXX/RequestToken")
if (connection.responseCode == HTTP_OK) {
String authToken = authTokenFromJson(jsonFromDssRequestTokenConnection(connection))
log.info("Authentication token: $authToken")
println(connection.getInputStream().getText());
if (connection.responseCode == HTTP_OK) {
println("Got http response code: ${connection.responseCode}, message: ${connection.responseMessage}")
log.info("Successful DSS request to ${connection.getURL()}")
LazyMap json = jsonFromDssExtractionConnection(connection)
.....
}
private static String authTokenFromJson(LazyMap json) {
json.value
}
private static LazyMap jsonFromDssRequestTokenConnection(HttpURLConnection connection) {
connection.inputStream.withCloseable { inStream ->
new JsonSlurper().parse(inStream as InputStream)
} as LazyMap
}
解决方案
推荐阅读
- apache-spark - 当我们尝试将 Spark DataFrame 写入 Cassandra 时,Cassandra 类型如何在内部转换数据类型?
- python - 使用多个时间戳对多个用户数据进行分组
- wpf - 如果用户输入了无效的电子邮件地址,则禁用按钮 - WPF
- javascript - AJAX JSON 加载的表单按钮刷新页面而不是再次使用 AJAX
- php - PHP 致命错误:未捕获的异常:错误:1408F10B:SSL 例程:ssl3_get_record:/api.genius.com 的版本号错误
- javascript - 更新 Dynamodb 中的项目?
- python - 使用shutil和重命名时出错
- rust - 如何使 #![no_main] 属性有条件?
- javascript - 需要为多个搜索文件夹创建搜索值为 null
- google-analytics - Google Analytics(分析)命中未发送