2011-07-14 4 views
0

は、私は、高速文字列にインストリームに変換するにはどうすればよい今..インストリームを文字列をより速く変換する(Android)?

private static String convertStreamToString(InputStream is) { 
    /* 
    * To convert the InputStream to String we use the BufferedReader.readLine() 
    * method. We iterate until the BufferedReader return null which means 
    * there's no more data to read. Each line will appended to a StringBuilder 
    * and returned as String. 
    */ 
    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(); 
} 

を使用してイムしかし、これはエミュレータ上の少なくとも2〜3分かかります。私はインストリームをバイト配列に変換してから文字列に変換すれば高速になることがわかった。誰にもこれについての知識はありますか?

+0

@Philip どうすればよいですか? – user758114

答えて

0

代わりにByteArrayOutputStreamを使用してください。

+0

これは役に立ちます 応答= httpclient.execute(httpget); if(response.getStatusLine()。getStatusCode()== 200){ //接続が確立されました。コンテンツを取得します。 \t ByteArrayOutputStream i =(ByteArrayOutputStream)response.getEntity(); – user758114