gpt4 book ai didi

java - 什么时候 DownloadManager 优于 HttpUrlConnection?

转载 作者:行者123 更新时间:2023-11-29 21:10:41 24 4
gpt4 key购买 nike

我正在从 Web API 请求纯文本数据,这些数据可以大到 ~5 MB 或小到 ~1 KB。

我对使用 HttpUrlConnection 犹豫不决,因为超时的可能性增加,而且 5 MB InputStream 的内存占用。

来自 Android 文档:

下载管理器是处理长时间运行的 HTTP 下载的系统服务。

5 MB 算作“长时间运行”吗? HttpUrlConnection 的预期用途似乎是用于小数据包,而不是 5 MB 的文本。

最佳答案

I'm hesitant to use HttpUrlConnection because of the increased likelihood of timing out

我不确定您对超时有何担忧。只要您以合理的速度下载数据,我不明白为什么您的服务器会放弃您的连接。

现在,您可能会遇到用户移动并最终断开连接的问题(例如,从 WiFi 故障转移到移动数据)。

and the memory footprint of a 5 MB InputStream

InputStream 不是 5MB。对于这种大小的内容,您通常一次读取几 KB 并将结果写入文件。

Does 5 MB count as "long-running?"

这取决于你。它足够大,我会考虑 DownloadManager

It seems that the intended use of HttpUrlConnection is for small packets of data, not 5 MB of text.

HttpURLConnection 用于从 1 字节到 1 TB 甚至理论上更高的所有内容。 DownloadManager 可能使用 HttpURLConnection

DownloadManager 为您做的是:

  • 处理连接故障转移并获取下载
  • 在下载过程中保持设备唤醒
  • 等等

关于java - 什么时候 DownloadManager 优于 HttpUrlConnection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22976310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com