【问题标题】:Receiving chunked data endlessly无休止地接收分块数据
【发布时间】:2011-10-27 22:48:25
【问题描述】:

您可能知道在 HTTP 标头中发送分块文件,没有内容长度,因此程序必须等待 0 才能理解该文件已结束。

--sample http header  
POST /some/path HTTP/1.1
Host: www.example.com  
Content-Type: text/plain  
Transfer-Encoding: chunked  
25  
This is the data in the first chunk  
8
sequence  
0

为了接收这个文件,可以使用以下代码。

ResponseHandler<String> reshandler = new ResponseHandler<String>() {
    public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

        HttpEntity entity = response.getEntity();

        InputStream in =  entity.getContent();
        byte[] b = new byte[500];
        StringBuffer out = new StringBuffer();
        int len = in.read(b);
        out.append(new String(b, 0 , len));

        return out.toString();
    }
}; 

但就我而言,我使用流媒体频道,换句话说,没有 0 表明该文件已结束。无论如何,如果我使用此代码,它似乎永远等待等待永远不会发生的 0。我的问题有没有更好的方法从流通道接收分块文件?

【问题讨论】:

    标签: android http chunked-encoding chunked


    【解决方案1】:

    好的。我可以使用简单的方法接收数据(如下所示,不使用响应处理程序)。无论如何,我仍然对apache ChunkedInputStream 以及它如何有用而正常输入流可以处理分块数据感到有些困惑。

    is =  entity.getContent();
    StringBuffer out = new StringBuffer();
    byte[] b = new byte[800];
    int len = is.read(b);
    out.append(new String(b, 0 , len));
    result = out.toString();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多