问题描述
下面code是工作的罚款中的Android 1.5-2.2.1,但它不是在2.3及更高版本。
The following code is working fine in Android 1.5-2.2.1 but it's not in 2.3 and higher.
BufferedReader rd;
rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null){
sb.append(line);
}
rd.close();
在stracktrace:
The stracktrace:
01-30 08:21:42.668: WARN/System.err(594): java.io.IOException: BufferedInputStream is closed
01-30 08:21:42.668: WARN/System.err(594): at java.io.BufferedInputStream.streamClosed(BufferedInputStream.java:116)
01-30 08:21:42.728: WARN/System.err(594): at java.io.BufferedInputStream.read(BufferedInputStream.java:274)
01-30 08:21:42.728: WARN/System.err(594): at org.apache.harmony.luni.internal.net.www.protocol.http.UnknownLengthHttpInputStream.read(UnknownLengthHttpInputStream.java:40)
01-30 08:21:42.758: WARN/System.err(594): at java.io.InputStreamReader.read(InputStreamReader.java:255)
01-30 08:21:42.758: WARN/System.err(594): at java.io.BufferedReader.fillBuf(BufferedReader.java:128)
01-30 08:21:42.758: WARN/System.err(594): at java.io.BufferedReader.readLine(BufferedReader.java:357)
这是一个问题? 2.3有什么改变?
Is this an issue? Has anything changed in 2.3??
推荐答案
我注意到了这一点。有一件事你可以做的是检查你的循环结束时的BufferedReader的状态:
I noticed this as well. One thing you can do is check the state of the BufferedReader at the end of your loop:
while((line = rd.readLine()) != null){
sb.append(line);
if (!rd.ready()) {
break;
}
}
rd.close();
不过,就绪()并不能保证你的数据流是在年底。它只有在流被阻断或关闭检查。请参阅BufferedReader.ready()在developer.android.com
However, ready() does not guarantee that your stream is at the end. It only checks if the stream is blocked or closed.See BufferedReader.ready() on developer.android.com
我觉得这是Android操作系统2.3和上面介绍的错误。请有助于在Android官方网站上的错误基地,鼓励谷歌解决这个问题。不幸的是,那些已经随操作系统版本2.3到2.3.3已经有很多设备。
I think this is a bug introduced in Android OS 2.3 and above. Please contribute to the bug base on the the official android site to encourage Google to fix this problem. Unfortunately, there are already many devices that have already shipped with OS versions 2.3 to 2.3.3.
您的情况是,你可能已经打了文件的末尾,在一读,流将自动关闭。我认为这是Android的2.3和上面介绍的错误。
Your situation is that you've probably hit the end of the file in one read and the stream is automatically closed. I think that is a bug introduced in Android 2.3 and above.
glassonion的帖子会的工作,但解决方法将在较长的数据流进行得非常缓慢。
glassonion's post will work, but that workaround will perform very slowly on long streams of data.
由于移动应用程序可以在任何时候失去互联网连接,你从阅读的InputStream可能只是简单地被阻塞,等待更多的数据。
Since mobile applications may lose internet connectivity at any time, the InputStream you're reading from may just simply be blocked and waiting for more data.
一个更恰当的做法可能是在一个新的线程打开一个套接字,读取数据,并等待一个超时以对冲任何进展缓慢的连接。
A more proper approach may be to open a socket on a new thread, read the data and wait with a timeout to hedge against any slowness in your connection.
下面的文章解释了处理停滞的连接相当不错。读总结应该足以说服你。
The following article explains handling stalled connections quite well. Reading the summary should be enough to convince you.
所有优秀的网络应用将包括超时检测和处理。无论你正在写一个客户端,并且需要检测一个任性的服务器,或者写一个服务器,并需要prevent停滞连接,超时处理是错误处理的重要组成部分。的
这篇关于java.io.IOException异常:的BufferedInputStream是Android 2.3的封闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!