while ((line = br.readLine()) != null) {
当执行下面完整代码的BufferedReader.readLine() 时,我得到了一个 IOException 。Exception.getMessage()
返回的BufferedInputStream 是关闭的。
它只发生在 HTC 设备中,当我使用索尼爱立信 XPERIA 时它不会发生。
我的完整代码:
public static String downloadString(String url) throws MalformedURLException, IOException {
InputStream is = downloadStream(url);
//Convert stream into String
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(is), 4096);
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
}
public static InputStream downloadStream(String url) throws MalformedURLException, IOException {
return connection(url).getInputStream();
}
private static HttpURLConnection connection(String s_url) throws MalformedURLException, IOException {
URL url = new URL(s_url);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
return urlConnection;
}
如何制作适用于每台设备的更好的 downloadString 方法?
提前致谢。非常感谢您的回答