我试图获得股票的最新股票价格。我使用的URL是
String stockURL=http://www.google.com/ig/api?stock=ADSL
。
我可以在浏览器中打开此链接。我编写了以下代码,以便解析执行URL后得到的文档。
public static void main(String[] args) {
URL url;
try {
url = new URL(stockUrl);
GettingCurrentStockPrice gettingCurrentStockPrice = new GettingCurrentStockPrice();
gettingCurrentStockPrice.readDoc(gettingCurrentStockPrice
.parse(url));
} catch (MalformedURLException e) {
System.out.println("Encountered MalformedURLException:" + e);
} catch (DocumentException e) {
System.out.println("Encountered DocumentException:" + e);
}
}
private Document parse(URL url) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(url);
return document;
}
private void readDoc(Document document) {
Element root = document.getRootElement();
// iterate through child elements of root
for (Iterator<Element> i = root.elementIterator(); i.hasNext();) {
Element element = (Element) i.next();
System.out.println(element);
}
for (Iterator i = root.elementIterator("company data"); i.hasNext();) {
Element compName = (Element) i.next();
System.out.println("compName-->" + compName);
}
}
在运行程序时,结束于,
Encountered DocumentException:org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect
。
为什么无法打开指定的网址?
更新:问题出现在parse()
行render.read(url);
。