Android Download Progress

旧巷老猫 提交于 2019-11-28 19:56:24

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/2100737/android-download-progress

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!