Question

I'm requesting text-only data from a web API that can be as large as ~5 MB or as small as a ~1 KB.

I'm hesitant to use HttpUrlConnection because of the increased likelihood of timing out and the memory footprint of a 5 MB InputStream.

From the Android docs:

The download manager is a system service that handles long-running HTTP downloads.

Does 5 MB count as "long-running?" It seems that the intended use of HttpUrlConnection is for small packets of data, not 5 MB of text.

Was it helpful?

Solution

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

I'm not sure what your concern is regarding timing out. So long as you are downloading the data at a reasonable pace, I don't see why your server would give up on your connection.

Now, you may run into problems of the user moving and winding up dropping your connection (e.g., fail over from WiFi to mobile data).

and the memory footprint of a 5 MB InputStream

An InputStream is not 5MB. For something of that size, you usually read in several KB at a time and write the results to a file.

Does 5 MB count as "long-running?"

That's up to you. It's big enough that I'd consider DownloadManager.

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

HttpURLConnection is used for everything from one byte to one terabyte and theoretically beyond. DownloadManager probably uses HttpURLConnection.

What DownloadManager does for you is:

  • Handle connectivity failover and picking up the download
  • Keeping the device awake while the download is going on
  • Etc.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top