是否有任何Android开发过成功接收块传输协议,从Web服务? [英] Have any Android developers had success receiving chunked transfer protocol from a web service?

查看:125
本文介绍了是否有任何Android开发过成功接收块传输协议,从Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力与几个类实现检索分块数据没有成功。以下是有问题的简化code模块。各地的网络冲浪之后,似乎出现了过去的问题(2009年,2010,版本1.1,1.5),但它们应该是由现在已经得到解决。我还没有看到任何明确的成功,Android平台的这一协议。

帮助!

我能看到一些反应,如果我把一个无效的令牌 - Web服务将一个应用程序错误消息作出回应。然而,有效的URL和令牌会简单地用一个检测分块协议(isChunked()返回true)的反应,但没有被读取,而什么时候出,等等。

这是一个命令行发出的卷曲完全相同的网址运行正常,并显示(由Web服务发布的数据)连续的内容。

是否有任何Web服务端的黑客,例如,增加终端的线越多,给力接收流??

  URI URI;
                尝试 {
                    URI =新URI("http://cws.mycompany.com/service/events?accesskeyid=8226f3ddc65a420abc391d8f1fe12de44766146762_1298174060748");
                    HttpClient的HttpClient的=新DefaultHttpClient();
                    HTTPGET HTTPGET =新HTTPGET(URI);
                    ResponseHandler的<字符串> RH =新BasicResponseHandler();
                    字符串responseString = httpClient.execute(HTTPGET,RH);
                    Log.d(TAG,响应,字符串:\ N+ responseString);
                }赶上(的URISyntaxException E){
                    Log.e(TAG,e.toString());
                    e.printStackTrace();
                }赶上(ClientProtocolException E){
                    Log.e(TAG,e.toString());
                    e.printStackTrace();
                }赶上(IOException异常E){
                    Log.e(TAG,e.toString());
                    e.printStackTrace();
                }
 

解决方案

我测试过了,你写了我的仿真器与Android 2.2 code和它工作正常。我用的是分块的网址是:

  URI =新的URI(http://www.httpwatch.com/httpgallery/chunked/);
 

我注意到 BasicResponseHandler 继续尝试读取,直到它到达流的末尾,并给出了当时整个数据的一次。在code都挺等待流关闭。请问您的Web服务端的数据流?或是否继续回馈块下去吗?我没有看到一个方法来取回只是第一分块,但我没有写,只是读取输入流(这给予了足够大的缓冲区对应块)第一次读一个简单的处理器。在我用于测试的URI的情况下,它返回HTML文件作为一个chunk的每一行。你可以看到刚才第一个回到这里。

如果这对你的作品,那么你可以很容易地编写一个处理程序,返回而不是字符串,返回一个枚举或其他对象,你可以返回每个块。甚至你自己的类。

 公共类ChunkedResponseHandler实现ResponseHandler的<字符串> {
    公共字符串用handleResponse(Htt的presponse响应)抛出ClientProtocolException,IOException异常{

        HttpEntity实体= response.getEntity();
        InputStream的时间= entity.getContent();
        StringBuffer的OUT =新的StringBuffer();
        byte []的B =新的字节[4096];
        INT N = in.read(B);
        out.append(新的String(B,O,N));
        返回out.toString();
    }
}
 

I have struggled with several class implementations to retrieve chunked data without success. Following is a simplified code module that has the problem. After surfing around the web, it appears there have been problems in the past (2009, 2010; ver 1.1, 1.5), but they should be resolved by now. I have not seen any definitive success with Android platform for this protocol.

Help!

I am able to see some response if I put an invalid token -- the web service will respond with an application error message. However, the valid url and token will simply respond with a detection of the chunked protocol (isChunked() returns true), but nothing gets read and nothing times-out, etc.

The exact same URL issued with CURL from a command line works as expected and displays the continuous content (published data from web service).

Are there any web service side hacks e.g., add more end-of-lines, to force the receiving stream??

                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();
                }

解决方案

I've tested out the code that you wrote on my emulator with Android 2.2 and it works fine. The chunked url I was using was:

        uri = new URI("http://www.httpwatch.com/httpgallery/chunked/");

I noticed that the BasicResponseHandler continues to try to read until it reaches the end of stream, and gives back then entire data all at once. The code could hang waiting for the stream to close. Does your webservice end the stream? or does it continue to give back chunks forever? I don't see a method to get back just the first chunked, but I did write a simple handler that just reads the first read from the input stream (which given a large enough buffer corresponds to the chunk). In the case of the URI I used for testing, it returns each line of the HTML file as a chunk. You can see just the first one returned here.

If this works for you, then you could easily write a handler that returns instead of a string, returns an Enumeration or some other object where you could return each of the chunks. Or even your own class.

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();
    }
}

这篇关于是否有任何Android开发过成功接收块传输协议,从Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆