Friday, February 21, 2014

Dynamic ListView: AsyncTask Comparison by JFrankie

In this post, I will describe first how to use Volley to populate dynamically a ListView, and then compare it against AsycnTask.

In this post, we will invoke a remote service to get contact information in JSON format, and we populate the items inside a ListView.This simple example is very useful to understand Volley library and how we can integrate it. We want to obtain something like the image shown below:

We have already covered this topic in a previous post, and this is the chance to underline the different approaches (Volley and AsyncTask).

Volley JSONArrayRequest


Inside Volley, there is a class called JsonArrayRequest, that can be used to retrieve JSON Array as response. As always, we have two different listener; we have to implement:

one to get the response
another one to handle errors

The source code is shown below:

JsonArrayRequest jReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {

@Override
public void onResponse(JSONArray response) {
List<Contact> result = new ArrayList<Contact>();
for (int i = 0; i < response.length(); i++) {
try {
result.add(convertContact(response
.getJSONObject(i)));
} catch (JSONException e) {
}
}
adpt.setItemList(result);
adpt.notifyDataSetChanged();
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// Handle error

}
});



In onResponse method, we have a JSONArray object that holds the response. In this method, we traverse the json array and convert json data to Contact object, using convert method:

private final Contact convertContact(JSONObject obj) throws JSONException {
String name = obj.getString("name");
String surname = obj.getString("surname");
String email = obj.getString("email");
String phoneNum = obj.getString("phoneNum");

return new Contact(name, surname, email, phoneNum);
}


At the end, we set the new contact list in our adapter and call:

Usage Example by Paresh Mayani

I am not sure whether you have heard “Volley” word yet but it’s the library on which one expert talk was delivered during Google I/O 2013 by Ficus Kirkpatrick.

What is Volley library exactly for?

Volley is a library that makes networking for Android apps easier and most importantly, faster.
It manages the processing and caching of network requests and it saves developers valuable time from writing the same network call/cache code again and again. And one more benefit of having less code is less number of bugs and that’s all developers want and aim for :)
My mean of writing the same network call code is AsyncTask and the logic/code you write for fetching response from Web API and displaying it in particular View. We have to take care of displaying ProgressBar/ProgressDialog inside onPreExecute() and onPostExecute(). I know this is not a hard task but still boring, sometimes I also feel get bored even though I have defined BaseTask class for managing display/dismiss operation of ProgressBar/ProgressDialog and many more thing. So now we can say Volley can be a powerful alternative of AsyncTask.

Advantages of using Volley:

  1. Volley automatically schedule all network requests. It means that Volley will be taking care of all the network requests your app executes for fetching response or image from web.
  2. Volley provides transparent disk and memory caching.
  3. Volley provides powerful cancellation request API. It means that you can cancel a single request or you can set blocks or scopes of requests to cancel.
  4. Volley provides powerful customization abilities.
  5. Volley provides Debugging and tracing tools

How to get started?

  1. Clone the Volley project
  2. Import the code into your project
Clone the Volley project:
git clone https://android.googlesource.com/platform/frameworks/volley

Read More

.

Why & How to use Volley to make Android apps faster


Volley is a library that makes networking for Android apps easier and most importantly, faster. We'll give an overview of how it works, common patterns that work well with it, and a walkthrough of how you can easily load thumbnail images for your ListView from the network in parallel.



This is an excellent explanation from Google Developers team about Android Valley with examples through video