0
以下構造方法應將URL中的XML讀取到XML Document
對象中。雖然它已經有效,但我仍然懷疑它是正確的。HttpURLConnection的InputStream:何時斷開?
// Basic constructor method without exception handling
Feed(URL url) throws IOException, ParserConfigurationException, SAXException {
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
httpcon.addRequestProperty("User-Agent", "Some User-Agent");
InputStream inStream = httpcon.getInputStream();
httpcon.disconnect();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(inStream);
}
問題:
- 不應該一個第一解析
InputStream
,然後關閉HttpURLConnection
? - 不應該有
httpcon.connect()
之前我試圖get
東西從httpcon
?