从java中的url / HTTP读取xml文件

时间:2016-12-06 12:03:53

标签: java xml

你好我有一个大问题。我必须从URL / HTTP解析XML文件,但它不起作用。

代码:

try {           

    String url = "http://api.nbp.pl/api/exchangerates/rates/a/gbp/2012-01-01/2012-01-31/";
    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    f.setNamespaceAware(false);
    f.setValidating(false);
    DocumentBuilder b = f.newDocumentBuilder();
    URLConnection urlConnection = new URL(url).openConnection();
    urlConnection.addRequestProperty("Accept", "application/xml");
    Document doc = b.parse(urlConnection.getInputStream());
    doc.getDocumentElement().normalize();

    System.out.println ("Root element: " +  doc.getDocumentElement().getNodeName());
    } catch (Exception e) {
        System.out.println(e.getMessage());
}

错误:

Connection timed out: connect

0 个答案:

没有答案