15

In an Android app, I have to make multiple GET requests to a URL in order to transmit data to an external server (that's how the third party API works).

Data comes in sporadically. I store this data in a queue and want to send it to the server in a background asynchronously without slowing down the main UI thread. Each unit of data in the queue requires a GET request. The queue has to be emptied even if the app closes.

What's the best practice to do this? Please post/direct me to code/tutorials.

ask
  • 2,160
  • 7
  • 31
  • 42

2 Answers2

33

What's the best practice to do this?

That would depend on what "this" is and where this work is being done.

If "this" is "asynchronous work", you will use threads in one form or fashion:

  • If your HTTP operations are driving a UI, you might use AsyncTask, so you can update the UI safely from onPostExecute()

  • If your HTTP operations are purely in the background, and you want to do one at a time, use an IntentService, which has its own background thread and work queue

  • If your HTTP operations are purely in the background, and you want to do one at at time, and you are concerned about ensuring that the device should stay awake while all this is going on, consider my WakefulIntentService

  • If your HTTP operations are purely in the background, but you feel that you want to do several at a time, roll your own Service that uses an Executor with your own thread pool, making sure that you shut down that service when the work is done (as IntentService does), and making sure that the device stays awake with a WakeLock (and perhaps a WifiLock)

  • Etc.

If "this" is "HTTP GET" requests, use:

  • HttpUrlConnection, or

  • HttpClient, or

  • OkHttp (wrapper around those with added benefits), or

  • Retrofit (if your GET requests are really Web service calls), or

  • Volley (if you like your HTTP wrapper code to be undocumented, unsupported, but Googly)

  • Any number of other third-party wrapper libraries

If "this" is "queue", use the Queue class, or LinkedBlockingQueue if you plan on having multiple threads work with it at once.

If "this" is something else, I can't help you, as I'm tired of guessing.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Note: Volley is now both recommended best practice, supported, documented and does not only apply for GET requests. – Nilzor May 28 '14 at 12:20
  • 1
    @Nilzor: "recommended best practice" -- please provide links to pages on http://developer.android.com that indicate this. "supported, documented" -- please provide links to some site (e.g., GitHub repo) where the support is taking place and the documentation resides. "does not only apply for GET requests" -- I never said that it was *only* for `GET` requests. The *question* is about `GET` requests. – CommonsWare May 28 '14 at 12:26
  • I was a bit quick there and thought you were promoting HTTP calls in AsyncTasks, which you are not. It unfortunately seems like Google still is promoting that themselves (http://developer.android.com/training/basics/network-ops/connecting.html), which for most use cases (e.g. JSON requests) is a bad practice in terms of performance and reliability. You're right it's still not supported officially by the Android team nor documented. – Nilzor May 28 '14 at 12:53
8

You could do your own async HTTP GET calls using AsyncTask but I would recommend against it unless you're doing it from a learning point of view. If you want a nice, clean and stable solution I'd suggest that you use the well known Android Asynchronous Http Client 3rd party library. From the site:

"An asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries. All requests are made outside of your app’s main UI thread, but any callback logic will be executed on the same thread as the callback was created using Android’s Handler message passing."

Making a GET is as easy as:

AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(String response) {
        System.out.println(response);
    }
});
britzl
  • 10,132
  • 7
  • 41
  • 38