Search code examples
javaandroidloopj

What is the most optimal way to upload large data to server from an Android App?


I have an Android App that needs to upload, download data files between 10-12MB relatively often. Given that my app is more popular in countries like India which may not have good internet connectivity I am working under following constraints

  • User may not be connected to internet 24x7
  • User's interaction does not depend on whether the download/upload succeeds.

This is a poems app where we download new poems to users phone and upload user's poems to our server whenever we are connected to internet.

I understand that using "com.loopj.android.http.AsyncHttpClient;" for the communication but I was wondering if there are any better alternatives which can do the following:

  • Should I start a service which provides interfaces for upload/download ?
  • Is it possible to do this stuff in background, even when user is not using the app?

Solution

  • Yes, you should use a service, in particular a foreground service (to show a notification as the upload progresses). Additionally, a foreground service is not likely to be killed by Android:

    ... the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory.

    For downloads, Android provides a ready-made solution called DownloadManager. It already includes the capability to resume downloads in case the connection is lost.

    For uploads, there is no equivalent (at least as far as I know). You would have to roll out your own solution, probably involving the Content-Range HTTP header. It depends a lot on your server, for example Google Drive has an API for this. Also take a look at Resume file upload/download after lost connection (Socket programming) (although it's with sockets, the concepts are more or less the same).

    Another options is to use a SyncAdapter for this task. See Transferring Data using Sync Adapters. I'm not sure if this supports resuming though.