Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley and AsyncTask

I read a post about Volley and I know it's great networking library. But I couldn't understand one thing.

All requests are Async Task or not?

When I want to send asyncTask request using Volley do I need put Volley request in AsyncTask? or should I just call Volley Request if it is already AsyncTask request?

 private class MyClass extends AsyncTask<String, Void, String> {          @Override         protected String doInBackground(String... params) {            // do Volley request         } } 

Is this right approach?

like image 496
pmb Avatar asked Dec 19 '13 06:12

pmb


People also ask

What is AsyncTask?

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

What is the difference between retrofit and volley?

Retrofit has full support for POST requests and multi part file uploads, with a sweet API to boot. Volley supports POST requests but you'll have to convert your Java objects to JSONObjects yourself (e.g., with Gson). Also supports multi part requests but you need to add these additional classes or equivalent.

Is AsyncTask deprecated?

AsyncTask deprecated alternative Android – Java private final Executor executor = Executors. newSingleThreadExecutor(); A Handler gives you a mechanism to push tasks into the UI thread queue from any other threads thus allowing other threads to communicate with the UI thread.

Which of the following is are benefits of using volley compared to AsyncTask in terms of performing network operations?

A major advantage of Android Volley over AsyncTask is that you can do multiple requests simultaneously without the overhead of thread management.


1 Answers

You don't need to run Volley request on AsyncTask.

Why:

They manage all network related task on separate thread. If you look closely at library project they did not picture the AsyncTask. But they intelligently handle all network related task efficiently.

Check RequestQueue.java class in Volley's main package

here I am pasting java doc.

/**  * A request dispatch queue with a thread pool of dispatchers.  *  * Calling {@link #add(Request)} will enqueue the given Request for dispatch,  * resolving from either cache or network on a worker thread, and then delivering  * a parsed response on the main thread.  */ 

Edited:

Forming a Request:

With Volley, network communication is managed by the RequestQueue. The best way to utilize the RequestQueue and all of its tools, especially the cache, is by instantiating it once and keeping it around as a singleton. At this point you can then add or cancel requests, stop or start requests, and access the response cache(s).

RequestQueue queue =Volley.newRequestQueue(this); 

Once the RequestQueue has been instantiated a request must be formed. This can be done utilizing a few different “out of the box” request classes included with the Volley Library or by extending Volley’s request class into your own custom request. The request classes already included in Volley are a String request, JSON requests, and an Image Request. Most of the request classes included in Volley library utilize constructors much like the one below.

Parameters being passed into constructor:

RequestMethod(get, post, delete, ect) JSONObject-An optional object that will be posted with your request ResponseListener- Where your data will go after the request is complete ErrorListener – What will be told when there was a problem with your request.

JsonObjectRequest request = JsonObjectRequest(Requestmethod, url, null,  new ResponseListener(), new ErrorListener()); 

Listners to receive response:

Successful Response Listener

private class ResponseListener implements Response.Listener{   @Override   public void onResponse(JSONObject response){    } } 

Error Response Listener

private class ErrorListener implements Response.ErrorListener{   @Override   public void onErrorResponse(VolleyError error){    } } 

Finally add your request to Request queue, rest of everything Volley will handle for you.

Making call:

Now, that we have made our request and response classes we are ready to add the request to the queue and retrieve the data. To do so we simply add the request to the queue.

queue.add(request); 

The response or error will then be delivered to the response/error classes that we defined in our request. You can add as many requests to the queue that you would like at one time and the responses will be delivered to their respective response/error classes

like image 166
Gru Avatar answered Sep 23 '22 18:09

Gru