有没有Android开发人员成功从Web服务接收分块传输协议?

mob*_*bob 9 android http chunked http-chunked

我一直在努力处理几个类实现,以检索分块数据但没有成功.以下是一个有问题的简化代码模块.在网上浏览之后,过去似乎出现了问题(2009年,2010年;版本1.1,1.5),但现在应该解决这些问题.我没有看到Android平台对此协议有任何明确的成功.

救命!

如果我输入无效令牌,我能看到一些响应 - Web服务将响应应用程序错误消息.但是,有效的url和令牌只会响应检测到的chunked协议(isChunked()返回true),但没有任何内容被读取,也没有任何超时等等.

从命令行使用CURL发出的完全相同的URL按预期工作,并显示连续内容(来自Web服务的已发布数据).

是否存在任何Web服务端黑客,例如,添加更多行尾,强制接收流?

                URI uri;
                try {
                    uri = new URI("http://cws.mycompany.com/service/events?accesskeyid=8226f3ddc65a420abc391d8f1fe12de44766146762_1298174060748");
                    HttpClient httpClient=new DefaultHttpClient(); 
                    HttpGet httpGet=new HttpGet(uri); 
                    ResponseHandler<String> rh=new BasicResponseHandler(); 
                    String responseString=httpClient.execute(httpGet,rh); 
                    Log.d(TAG, "response as string:\n" + responseString);
                } catch (URISyntaxException e) {
                    Log.e(TAG, e.toString());
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    Log.e(TAG, e.toString());
                    e.printStackTrace();
                } catch (IOException e) {
                    Log.e(TAG, e.toString());
                    e.printStackTrace();
                }
Run Code Online (Sandbox Code Playgroud)

chr*_*ton 9

我已经测试了你在我的模拟器上用Android 2.2编写的代码,它运行正常.我使用的chunked url是:

        uri = new URI("http://www.httpwatch.com/httpgallery/chunked/");
Run Code Online (Sandbox Code Playgroud)

我注意到它BasicResponseHandler继续尝试读取,直到它到达流的末尾,并立即返回所有数据.代码可能会挂起,等待流关闭.您的Web服务是否结束了流?还是继续永远地回馈大块?我没有看到只返回第一个chunked的方法,但我确实编写了一个简单的处理程序,它只读取输入流中的第一个读取(给定一个足够大的缓冲区对应于块).对于我用于测试的URI,它将HTML文件的每一行作为块返回.你可以看到这里返回的第一个.

如果这对您有用,那么您可以轻松编写一个返回而不是字符串的处理程序,返回一个Enumeration或其他一些可以返回每个块的对象.甚至是你自己的班级.

public class ChunkedResponseHandler implements ResponseHandler<String> {
    public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

        HttpEntity entity = response.getEntity();
        InputStream in = entity.getContent();
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];
        int n =  in.read(b);
        out.append(new String(b, 0, n));        
        return out.toString();
    }
}
Run Code Online (Sandbox Code Playgroud)