以下构造方法应该将URL从URL读入XML Document
对象。虽然它已经有效,但我仍然怀疑它是否正确。
// 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
吗?答案 0 :(得分:1)
不应该首先解析InputStream然后关闭HttpURLConnection吗?
是,或者更确切地说是关闭InputStream.
在我尝试从httpcon获取内容之前,不应该有一个http on.connect()吗?
没有。它隐含在获取输入流中。
您发布的代码不正确,不应该有效。应在断开连接之前读取输入流。实际上,只有在你想要阻止连接池时才需要断开连接。