0

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);.

Anuj Balan
  • 7,629
  • 23
  • 58
  • 92

0 Answers0