安卓下载进度

发布于 2024-08-18 11:11:17 字数 759 浏览 3 评论 0原文

我想知道某些下载的进度。
当我从互联网上获取一些资源时, 像这样:

        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 in = httpConnection.getInputStream();”时被称为,
文件的所有内容都会立即下载。

如果我使用:

        htttpResponse = httpClient.execute(httpRequestBase);

问题是一样的。

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

谢谢大家!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○闲身 2024-08-25 11:11:17

我认为这已此处得到解答

简而言之:使用应位于标头字段中的“内容长度”。

编辑:抱歉,我没有准确回答问题。基本上,一旦您知道预期的总内容长度,您就可以跟踪收到的数据量并计算完成的百分比。

另请参阅: 如何创建下载管理器

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文