android:java.io.IOException:流已关闭

时间:2013-05-07 09:14:09

标签: android inputstream ioexception

我正在从服务器读取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文件中的前几条记录进行解析是成功的,然后异常即将到来

1 个答案:

答案 0 :(得分:0)

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