1

我的代码发送 HttpConnection 然后尝试使用connection.getInputStream()connection.getErrorStream()反序列化响应。

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // getErrorStream()
String content = in.lines().collect(Collectors.joining());

如何在完成()后将连接传递给parse(HttpURLConnection connection)函数并调用?connection.disconnect()

我在想

connection.disconnect();
return MyObject.fromString(connection.getInputStream(), connection.getErrorStream());

但不确定在阅读响应之前断开连接是否有意义。

4

1 回答 1

2

你可以这样做:

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String content = parse(connection);
connection.disconnect();
// do something with content
于 2019-08-23T21:01:49.330 回答