Stuck with connection timed out on trying to get and parse stock price

一笑奈何 提交于 2019-12-12 04:59:06

问题


Am trying to get the latest stock price of a stock. The URL am using as an example is

String stockURL=http://www.google.com/ig/api?stock=ADSL.

Am able to open this link in my browser. I have written the following code in order to parse the document which I get after executing the 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);
    }

}

On running the program, am ending with,

Encountered DocumentException:org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect.

Why is it unable to open the URL given?

Update: The problem comes in parse() line render.read(url);.

来源:https://stackoverflow.com/questions/11356448/stuck-with-connection-timed-out-on-trying-to-get-and-parse-stock-price

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!