2

I'm still new to Android, so, I'm still not so familiar with its libraries and APIs.

My first major project is a download manager which supports segmented downloading. I could already download files, but, I still have no idea on how or where to start for segmented downloading.

I have already browsed a lot of threads, but I really couldn't find any article or thread about segmented downloading in Android.

Can anyone please help me?

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
xRev
  • 21
  • 2

2 Answers2

2

This thread should help you : link

The idea is to use the Content-Range keyword in the header of your request (the server you are contacting has to be able to manage it)

sample code you can draw inspiration from :

public String readFirstChunk(String urlString, int byteCount) {
    String text = null;
    if (urlString != null) {
        org.restlet.Client restletClient = new org.restlet.Client(Protocol.HTTP);
        Request request = new Request(Method.GET, urlString);
        List<Range> ranges = Collections.singletonList(new Range(0, byteCount));
        request.setRanges(ranges);
        Response response = restletClient.handle(request);
        if (Status.SUCCESS_OK.equals(response.getStatus())) {
            text = processSuccessfulChunkRequest(response);
        } else if (Status.SUCCESS_PARTIAL_CONTENT .equals(response.getStatus())) {
            text = processSuccessfulChunkRequest(response);
        } else {
            System.err.println("FAILED "+response.getStatus());
        }
    }
    return text;
}

private String processSuccessfulChunkRequest(Response response) {
    String text = null;
    try {
        text = response.getEntity().getText();
    } catch (IOException e) {
        throw new RuntimeException("Cannot download chunk", e);
    }
    return text;
}
Community
  • 1
  • 1
Teovald
  • 4,369
  • 4
  • 26
  • 45
  • Does this implementation of segmented download use the android's download manager class? – xRev Sep 21 '12 at 06:16
0

You should study the Accept-Ranges header and also, the HttpClient class in Android.

Segmented downloading will just involve sending different values in the Accept-Ranges header via a request using HttpClient, receive partial content, and then merging all of the received content into one file.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191