Introducing Volley – A Networking Library

Have you ever heard of “Volley” word yet in android development? This tutorial is all about a library which google has delivered in Google I/O early in 2013. The library was given a name “Volley”

You probably have an idea what it is used for and what it is, if not you will have it by the end of this tutorial.

So, What is Volley library and for what it is used exactly?

Volley is a library that makes networking in Android faster and easier. It works in such a manner which saves our time from writing repetitive network code for processing and caching of network requests.

I use AsyncTask for network call code usually. We have to take care of displaying ProgressBar/ProgressDialog inside onPreExecute() and onPostExecute(). But now we can say that Volley can be a powerful replacement of AsyncTask.

Why use Volley:

1.) Volley automatically schedule all network requests.
2.) Volley provides transparent disk and memory caching.
3.) Volley provides powerful cancellation request API.
4.) Volley provides powerful customization abilities.
5.) Volley provides Debugging and tracing tools

To get started Clone the Volley project from GIT and import it into your project. You can clone it either by Git client or by Eclipse and ADT Bundle. Eclipse is having an option to clone repository and import project directly.

Let’s have a look on the classes of volley now. It includes 2 main classes:

Request queue : This class is used for dispatching requests to the network.

Request : It includes all the necessary details for making web API call. For example: which method to Use (GET or POST).

Follow the steps below to create an example using Volley:

Step 1: Make sure to import Volley projects into Eclipse. After importing, you need to make it a Library project by Right click => Properties => Android.

volley1

Step 2: Create a new project with the name VolleySampleProject, Right click on it and Include Volley Library in this project.

volley2

Step 3: Make sure you have includes INTERNET permission in your manifest file.

 

<uses-permission android:name="android.permission.INTERNET"/>

 

 

Step 4: Write following into your MainActivity.java :

 

package com.example.volleysampleproject;

import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends Activity {

private TextView txtToDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtToDisplay = (TextView) findViewById(R.id.txtToDisplay);

RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://en.wikipedia.org/wiki/Text_display";

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
// TODO Auto-generated method stub
txtToDisplay.setText("Response => "+response.toString());
findViewById(R.id.progressBar1).setVisibility(View.GONE);
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub

}
});

queue.add(jsObjRequest);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

 

 

Step5: Setup your layout xml and run for the output.

Check out my previous post on Android TV – Introduction.

Leave a Comment: