以下のコード全体のBufferedReader.readLine()を実行すると、IOExceptionが発生します(while ((line = br.readLine()) != null) {
)。 Exception.getMessage()
はを返します。BufferedInputStreamは、を閉じます。IOException BufferedInputStreamがHTCで閉じる
私はSony Ericsson XPERIAを使用すると、HTCデバイスでのみ起こります。
私の完全なコード:
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」リクエストメソッドを使用する場合は、ICSで始まるためurlConnection.setDoOutput(true)を使用しないでください。setDoOutputを使用すると、APIはリクエストメソッドを 'POST'に変更します)。 –