0

我正在从服务器读取 xml 数据并对其进行解析,在此期间我收到以下异常。

05-07 12:37:31.904: W/System.err(2765): java.io.IOException: stream closed
05-07 12:37:31.907: W/System.err(2765):     at libcore.net.http.AbstractHttpInputStream.checkNotClosed(AbstractHttpInputStream.java:68)
05-07 12:37:31.908: W/System.err(2765):     at  at libcore.net.http.ChunkedInputStream.read(ChunkedInputStream.java:41)
05-07 12:37:31.908: W/System.err(2765):     at java.io.InputStreamReader.read(InputStreamReader.java:244)
05-07 12:37:31.908: W/System.err(2765):     at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1496)
05-07 12:37:31.909: W/System.err(2765):     at org.kxml2.io.KXmlParser.readName(KXmlParser.java:1543)
05-07 12:37:31.909: W/System.err(2765):     at org.kxml2.io.KXmlParser.parseStartTag(KXmlParser.java:1045)
05-07 12:37:31.909: W/System.err(2765):     at org.kxml2.io.KXmlParser.next(KXmlParser.java:369)
05-07 12:37:31.909: W/System.err(2765):     at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)

我的代码是

**private static InputStream downloadUrl(String urlString) throws IOException {
        System.setProperty("http.keepAlive", "false");

**///// added after some search about this exception, but it didnt fix the issue fo**r me 

        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        //conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        return conn.getInputStream();
    }**


XmlPullParser parser = getParser(in);
parser.require(XmlPullParser.START_TAG, ns, "response");

            while (parser.next() != XmlPullParser.END_TAG) {
........
}

xml文件中的前几条记录解析成功,然后异常来了

4

1 回答 1

0

问题出在 XmlPullParser 上。下载大文件时有一些已知问题。我将 XML 解析更改为使用 DOM 。http://developer.android.com/reference/javax/xml/parsers/package-summary.html 它工作

于 2013-05-08T12:16:21.063 回答