Android的下载进度进度、Android

2023-09-05 06:02:26 作者:偷走你满目温柔

我想知道的一些下载进度。 当我得到一些资源来自互联网, 像这样的:

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
        }

当的InputStream中= httpConnection.getInputStream();被称为, 该文件的所有内容被下载一次。

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

如果我使用:

        htttpResponse = httpClient.execute(httpRequestBase);

的问题是相同的。

The problem is the same.

我如何检测的下载进度的实际情况?

How can I detect the actual progress of the download?

感谢大家!

推荐答案

我觉得这已经answered这里。

在简单地说:使用内容长度,这应该是在报头字段

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

编辑:对不起,我不回答这个问题完全。基本上,一旦你知道了预期的总长度的内容,那么你可以跟踪你有多少数据接收,并计算完成的百分比。

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.

另请参见:如何建立一个下载管理器