2011-03-10 19 views
1

我正在開發一個應用程序發佈到網站,我試圖將實體響應作爲字符串存儲。但是,字符串似乎只包含一小部分響應,大約35行左右。我想知道它是否與緩衝區溢出有關,但我真的不確定。我的代碼如下:使用緩衝區將http請求響應轉換爲Android中的字符串 - 未收到全部響應

static String getResponseBody(HttpResponse response) throws IllegalStateException, IOException{ 

    String content = null; 
    HttpEntity entity = response.getEntity(); 

    if (entity != null) 
    { 
     InputStream is = entity.getContent(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 

     while ((line = br.readLine()) != null) 
     { 
      if(isBlankString(line) == false) 
      { 
      sb.append(line + "\n"); 
      } 
     } 
     br.close(); 
     content = sb.toString(); 
    } 
    return content; 

isBlankString只是指出,如果線路不包含任何字符,因爲有很多的在被竊聽我的反應空行。我有沒有得到整個迴應的問題。任何機構知道發生了什麼或如何解決這個問題?

感謝

回答

8

在我的應用程序只使用單一線路從實體獲得響應字符串:

final String responseText = EntityUtils.toString(response.getEntity()); 
+1

哈哈,謝謝,這是一個大量簡單。但是,我認爲我還沒有得到全面的迴應。奇怪的是,如果我使用Log.v(DEBUG_TAG,responseText);在我調用你的代碼之後查看響應,我發現響應比將該字符串傳遞迴調用方法並將其連接到包含其他詳細信息(狀態行和一系列cookie)的字符串更多。是否有可能達到了字符串的最大長度或類似的東西? – 2011-03-14 16:16:03

+0

即使我面臨同樣的問題,請幫助 – 2012-08-10 14:31:56

+0

您確定這是迴應問題嗎?如果logcat過長,它會剪切字符串 – 2014-06-03 15:49:28