Stuck with a connection dedicated to trying to get and analyze stock price

I am trying to get the latest stock price. The URL is used as an example.

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

I can open this link in my browser. I wrote the following code to parse the document that I get after completing 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);
    }

}

When I start the program, I end up with

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

Why can't he open the specified URL?

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

0
source share

Source: https://habr.com/ru/post/1713152/


All Articles