Using Volley in Android application




Volley Library

Volley is an HTTP library developed by Google to ease networking tasks in Android Applications. Volley supersedes Java’s java.net.HttpURLConnection class and Apache’s org.apache.http.client in handling network requests. Volley can handle almost each and everything you will need to do over the network, it handles HTTP request and also manages the async tasks that you need to use while working with canonical networking classes.

Features of Android Volley Library :-
  1. Volley manages network request automatically and without you using any AsyncTask. It offers multiple concurrent network connections, resulting in faster and efficient network operations.
  2. Volley caches all network requests, utilizing the previous results in the case of change in activity configuration. However due to the same reason, it is only good for small Volley and not suitable for large download or streaming operations.
  3. It has a powerful cancellation API. You can fully control the request cancellation , or you can set blocks or scopes of requests to cancel.
  4. Volley allows you to control and order the network requests making it easy to populate UI elements in an orderly manner , useful in case of social networking apps like facebook , twitter. It also provides support for request prioritization.
  5. Volley has built in debugging and tracing tools which allow user to get to the root of the error.

Working of Volley.

While using volley you are only concerned about adding a request to the queue, handling it’s response, and you can actually ignore everything that is running in the background. When you fire a network request it is added to cache queue in the priority order. A cache dispatcher thread figures out whether it can service the request entirely from the cache or it needs to fire a network request for the response. In the case of a cache miss or if cached response has expired, the network dispatcher services the request, handles the parsing of the response and returns the response back to the main thread.
Volley is very useful for network request involving strings, images, and JSON. It has built in support for these objects.

Creating a New Project and Adding Volley

  1. Go to File → New → New Project and enter your Application Name.
  2. Enter Company Domain, this is used to uniquely identify your App’s package worldwide.
  3. Choose project location and minimum SDK and on the next screen choose Empty Activity, since we would be adding most of the code Ourselves. Then Click on Next.
  4. Choose an Activity Name. Make sure Generate Layout File check box is selected, Otherwise we have to generate it ourselves.Then click on Finish. We have left Activity Name as MainActivity.
  5. Gradle will configure your project and resolve the dependencies, Once it is complete proceed for next steps.
  6. To add Volley to your project add the following dependency in your App’s build.gradle file.
  7. 1
    compile 'com.android.volley:volley:1.0.0'

Add Internet Permission

Add the following permission to your AndroidManifest.xml file

AndroidManifest.xml


<uses-permission android:name="android.permission.INTERNET" />
In main Activity java file
For a string request
StringRequest getRequest = new StringRequest(Request.Method.GET, url,
                        new Response.Listener<String>()
                        {
                            @Override
                            public void onResponse(String response) {
                                // display response
                            }
                        },
                        new Response.ErrorListener()
                        {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                //
                            }
                        }
                );
For a JSONRequest
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
    new Response.Listener<JSONObject>() 
    {
        @Override
        public void onResponse(JSONObject response) {   
                        // display response     
            Log.d("Response", response.toString());
        }
    }, 
    new Response.ErrorListener() 
    {
         @Override
         public void onErrorResponse(VolleyError error) {            
            Log.d("Error.Response", response);
       }
    }
);
Add the request to a request queue
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(request_object);

The complete code will be:


                String url="http://httpbin.org/get?a=Nishanth";

                StringRequest getRequest = new StringRequest(Request.Method.GET, url,
                        new Response.Listener<String>()
                        {
                            @Override
                            public void onResponse(String response) {
                                 // do something after response is got
                            }
                        },
                        new Response.ErrorListener()
                        {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                            }
                        }
                );
                queue.add(getRequest);

Powered by Blogger.