Android Volley Example

Hello Readers,

Recently in Google I/O they have come up with a strong networking library named Volley.

Volley : Volley is a library that makes super fast networking for Android apps.

Now let me explain you how we can use volley in our android app.

Below are the steps in which I have covered a simple volley request example which will return JSON as an object and data will be displayed in list.

Step 1 : Download volley library

You can download the volley library from GIT

git clone https://android.googlesource.com/platform/frameworks/volley

Step 2 : Configure that project in your Android Project.

After downloading the repo create directory structure like com -> android -> volley

and copy the src folder from GIT repo.

volley_1

Step 3 : Create object of Reuest

RequestQueue reqQueue = Volley.newRequestQueue(this);

RequestQueue class allows you to create a request bundle object.

Step 4 : Create a request to get JSON reponse.

Volley has a beautiful feature which allows to Get JSON response from any givenURL

JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET, url,null, 
new Response.Listener() {                    
        @Override
        public void onResponse(JSONObject response) {
            // TODO Auto-generated method stub
              parseJSONRespone(response);
              ba.notifyDataSetChanged();
              progress.dismiss();
          }
        }, new Response.ErrorListener() {
                 @Override
                 public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        Log.v(TAG, error.getMessage());
                    }
         });

In above lines you can see that it has call back method which gives us the status of response
i.e.Response.Listener and Response.ErrorListener

Step 5 : Post the request

reqQueue.add(jr);

By simple adding request object in RequestQueue object library will start calling server for response.

Other things are same as we do like parsing JSON and displaying in either listview or any other View component.

Download an example here.

After successfully importing the project you can have below given view on your android device or emulator.

volley_2

Leave a comment