About Francesco Azzola

I'm a senior software engineer with more than 15 yrs old experience in JEE architecture. I'm SCEA certified (Sun Certified Enterprise Architect), SCWCD, SCJP. I'm an android enthusiast and i've worked for long time in the mobile development field.

Android Volley dynamic ListView: AsyncTask comparison

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:
 
 
 
 
 
volley_listview_dynamic[4]

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:

adpt.notifyDataSetChanged();

Volley vs AsyncTask

Now we are ready to compare Volley lib against AsycnTask. As we already know, AsyncTask have some problems regarding memory leaks, so we have to use it carefully.Volley doesn’t have such problems and we can use it every time we need to access to a remote resource.Moreover, Volley simplify the code we have to write to invoke a remote service. In the code below, I show these two different approaches:

private class AsyncListViewLoader extends AsyncTask<String, Void, List<Contact>> {
    private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);

    @Override
    protected void onPostExecute(List<Contact> result) {            
        super.onPostExecute(result);
        dialog.dismiss();
        adpt.setItemList(result);
        adpt.notifyDataSetChanged();
    }

    @Override
    protected void onPreExecute() {        
        super.onPreExecute();
        dialog.setMessage("Downloading contacts...");
        dialog.show();            
    }

    @Override
    protected List<Contact> doInBackground(String... params) {
        List<Contact> result = new ArrayList<Contact>();

        try {
            URL u = new URL(params[0]);

            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
            conn.setRequestMethod("GET");

            conn.connect();
            InputStream is = conn.getInputStream();

            // Read the stream
            byte[] b = new byte[1024];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            while ( is.read(b) != -1)
                baos.write(b);

            String JSONResp = new String(baos.toByteArray());

            JSONArray arr = new JSONArray(JSONResp);
            for (int i=0; i < arr.length(); i++) {
                result.add(convertContact(arr.getJSONObject(i)));
            }

            return result;
        }
        catch(Throwable t) {
            t.printStackTrace();
        }
        return null;
    }

}
RequestQueue rq = Volley.newRequestQueue(this);
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

            }
        });

rq.add(jReq);

It is clear that using Volley we have less code lines to write and we don’t have to worry about handling HTTP connection.
 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


eight − 6 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close