關閉我當執行BufferedReader.readLine(),在下面的完整代碼的行while ((line = br.readLine()) != null) {
)一個IOException。 Exception.getMessage()
返回 BufferedInputStream已關閉。IOException異常的BufferedInputStream在HTC
它只是發生在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?
在此先感謝。非常感謝您的回答
只是爲了確保,如果你想使用「GET」請求方法,不使用urlConnection.setDoOutput(真),因爲與ICS開始,API將改變請求方法爲「POST」如果你使用setDoOutput(真)。 – 2012-04-17 17:36:47