2011-03-10 85 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