【问题标题】:Using a buffer to convert http request responses to string in Android - Not getting entire response在Android中使用缓冲区将http请求响应转换为字符串-没有得到整个响应
【发布时间】:2011-07-13 00:15:42
【问题描述】:

我正在开发一个发布到网站的应用程序,并且我正在尝试将实体响应存储为字符串。但是,该字符串似乎只包含一小部分响应,大约 35 行左右。我想知道它是否与缓冲区溢出有关,但我真的不确定。我的代码如下:

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 只记录一行是否不包含任何字符,因为响应中有很多空白行让我烦恼。我有一个问题,不管有没有这个,我都没有得到整个回应。任何机构都知道发生了什么或如何解决这个问题?

谢谢

【问题讨论】:

    标签: android string httpresponse buffer-overflow bufferedreader


    【解决方案1】:

    在我的应用程序中,我只使用单行从实体获取响应字符串:

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

    【讨论】:

    • 哈哈,谢谢,简单多了。但是,我认为仍然没有得到完整的回应。奇怪的是,如果我使用 Log.v(DEBUG_TAG, responseText);要在调用您的代码后查看响应,与将字符串传递回调用方法并将其连接到包含其他详细信息(状态行和 cookie 列表)的字符串相比,我会看到更多的响应。我是否有可能达到字符串的最大长度或类似的东西?
    • 即使我也遇到同样的问题,请帮忙
    • 您确定这是响应问题吗?如果字符串太长,logcat 会截断字符串
    猜你喜欢
    • 2022-11-04
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 2020-07-06
    • 2012-12-18
    • 1970-01-01
    • 2012-11-21
    • 2023-02-15
    相关资源
    最近更新 更多