连接异常 - XMLPullParser

时间:2014-02-10 06:05:35

标签: java android httpurlconnection xmlpullparser

我正在尝试阅读BBC足球RSS。

以下是我尝试过的代码:

        URL url = new URL("http://feeds.bbci.co.uk/sport/0/football/rss.xml");        
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(false);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(url.openConnection().getInputStream(), "UTF_8"); // -- ERROR - Line 21

但是我得到了Connection异常。

以下是控制台中的例外情况:

java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at com.testing.code.RssParsing.main(RssParsing.java:21)

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

我使用此代码:

URL url = new URL("http://feeds.bbci.co.uk/sport/0/football/rss.xml");
    HttpURLConnection conn = (HttpURLConnection) 
    url.openConnection();
    conn.setReadTimeout(10000);
    conn.setConnectTimeout(15000);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    conn.connect();
    InputStream stream = conn.getInputStream();
    xmlFactoryObject = XmlPullParserFactory.newInstance();
    XmlPullParser xpp = xmlFactoryObject.newPullParser();
    xpp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
    xpp.setInput(stream, null);
    stream.close();

答案 1 :(得分:0)

设置以下属性可以解决问题......

System.setProperty("java.net.useSystemProxies", "true");