2012-04-16 7 views
1

以下のコード全体の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メソッドを作るには?

ありがとうございます。本当にあなたの答えに感謝

+0

「GET」リクエストメソッドを使用する場合は、ICSで始まるためurlConnection.setDoOutput(true)を使用しないでください。setDoOutputを使用すると、APIはリクエストメソッドを 'POST'に変更します)。 –

答えて

1

はそれを試す -

public static String getContent(String url) throws Exception { 
     return(new Scanner(new URL(url).openConnection().getInputStream()).useDelimiter("/z").next()); 
} 
+0

おかげで、このような簡単なコード!それはより効率的か、それとも何が違いますか? –

+0

このコードが役に立ちますので、問題が解決する可能性があります。 –

1

トライ使用HTTPGETを処理するために、HTTP GET接続(およびHttpPost HTTPのPOST処理するために)

をしていないときは常にストリームをクローズするようにしてください必要です。

ここで簡単なコードを使用HTTPGETサーバーから文字列を取得する:あなたはこのような関数を呼び出すことができます

private void executeRequest(HttpUriRequest request) 
{ 
    HttpClient client = new DefaultHttpClient(); 

    HttpResponse httpResponse; 
    try { 
     httpResponse = client.execute(request); 
     responseCode = httpResponse.getStatusLine().getStatusCode(); 
     message = httpResponse.getStatusLine().getReasonPhrase(); 

     System.out.println(responseCode + ":" +message); 

     HttpEntity entity = httpResponse.getEntity(); 

     if (entity != null) { 

      InputStream instream = entity.getContent(); 
      response = convertStreamToString(instream); 

      // Closing the input stream will trigger connection release 
      instream.close(); 
     } 

    } catch (ClientProtocolException e) { 
     client.getConnectionManager().shutdown(); 
     Toast.makeText(null, "Error", Toast.LENGTH_LONG); 
    } catch (IOException e) { 
     client.getConnectionManager().shutdown(); 
     Toast.makeText(null, "Error", Toast.LENGTH_LONG); 
    } catch (OAuthMessageSignerException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (OAuthExpectationFailedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (OAuthCommunicationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

private static String convertStreamToString(InputStream is) { 

    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 

HttpGet request = new HttpGet(url); 

executeRequest(リクエスト)。

関連する問題