Have any Android developers had success receiving chunked transfer protocol from a web service?

5.4k views Asked by At

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

There are 1 answers

5
christophercotton On BEST ANSWER

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