Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to send HTTP GET requests ansynchronously in Android?

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.

like image 792
ask Avatar asked Jun 05 '13 21:06

ask


People also ask

What is android async HTTP?

Overview. A popular third-party library called android-async-http helps handle the entire process of sending and parsing network requests for us in a more robust and easy-to-use way.

What is async HTTP?

Asynchronous HTTP Request Processing is a relatively new technique that allows you to process a single HTTP request using non-blocking I/O and, if desired in separate threads. Some refer to it as COMET capabilities.

Is Async task deprecated?

AsyncTask is used to perform time talking operations in android, but it's marked as deprecated from android 11.

What is http client android?

HttpClient is used when you want to receive and send data from the server over the internet. So for this you need to create a http client using HttpClient class. First, you will create the object of Http client and the URL to the constructor of HttpPost class that post the data.


2 Answers

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.

like image 96
CommonsWare Avatar answered Sep 19 '22 20:09

CommonsWare


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);
    }
});
like image 45
britzl Avatar answered Sep 21 '22 20:09

britzl