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