簡體   English   中英

我怎樣才能調用HttpUrlConnectiondetach()

[英]How can I call HttpUrlConnection disconnect()

我的代碼發送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());

但不確定在讀取響應之前斷開連接是否有意義。

您可以執行以下操作:

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String content = parse(connection);
connection.disconnect();
// do something with content

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM