23

I want to know the progress of some download.
When I'm getting some resource from the internet, like this:

        String myFeed = request.getURI().toString();
        URL url = new URL(myFeed);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        int responseCode = httpConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {

            InputStream in = httpConnection.getInputStream();
            // Process the input stream
        }

When "InputStream in = httpConnection.getInputStream();" is called,
all the content of the file is downloaded at once.

If I use:

        htttpResponse = httpClient.execute(httpRequestBase);

The problem is the same.

How can I detect the actual progress of the download?

Thank you all!

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Tsimmi
  • 1,828
  • 6
  • 23
  • 35

1 Answers1

9

I think this has been answered here.

In a nutshell: use the "Content-Length" which should be in the header fields.

EDIT: sorry, I wasn't answering the question exactly. Basically once you know the expected total content length then you can keep track of how much data you have received and calculate the percentage complete.

See also: How to create a download manager

Community
  • 1
  • 1
GaZ
  • 2,346
  • 23
  • 46
  • 1
    I already know the file size, through getContentLength(). What I don't know is how to get the progress of the content already received. I'm talking about just one file. – Tsimmi Jan 20 '10 at 11:41
  • Oh I see. Ok, well see the "how to create a download manager" link. Does that help? – GaZ Jan 20 '10 at 12:06
  • Sorry! I didn't saw that link at first. – Tsimmi Jan 20 '10 at 12:44
  • 1
    It wasn't there at first. I added it after your first comment ;) – GaZ Jan 20 '10 at 14:51