2

I have written program in android for downloading from web server I have Used JSON and Http request the program working properly... I want to show progress Bar with percentage depending upon things to be downloading. My query is how to increment the progress bar in percentage depending upon things to be downloaded and pending. I know how to calculate percentage but How would I know how much downloading is pending from server.

Sharath
  • 691
  • 8
  • 23
mayur rahatekar
  • 4,410
  • 12
  • 37
  • 51

1 Answers1

1

How much downloading is pending from server.

You have to implement a Range request in your application.

You may use this as a reference especially if you are using chunks. Otherwise, for all simple downloads

        URLConnection cxn = url.openConnection();
        cxn.connect();
        int lenghtBytes = cxn.getContentLength(); 

how to increment the progress bar

use publishProgress() in case of an AsyncTask

   publishProgress((int)(bytesDownloaded * 100 / lenghtBytes));
Reno
  • 33,594
  • 11
  • 89
  • 102
  • Thanks Reno for your reply, I know how to use Ayanc Task and publish progress but can you please tell me How to Implement Range Request in the Program. can I get this value from the server. – mayur rahatekar Oct 13 '11 at 05:43
  • 1
    There is a link to [DownloadManager.java](http://www.google.com/codesearch#rUC0lHK6Ryw/src/org/devtcg/five/util/streaming/DownloadManager.java) you can see the full implementation. If you want a simpler version, [refer to this answer](http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog/3028660#3028660) – Reno Oct 13 '11 at 05:47