Getting Started with Volley Library Android

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available through the open AOSP repository. By default all the volley network calls works asynchronously, so we don’t have to worry about using asynctask anymore.

Features of volley library

  • Automatic scheduling of network requests.
  • Multiple concurrent network connections.
  • Transparent disk and memory response caching with standard HTTP cache coherence.
  • Support for request prioritization.
  • Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
  • Ease of customization, for example, for retry and back off.
  • Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
  • Debugging and tracing tools.

Please look into the presentation of Volley library at Google I/O

Create Project With Volley Library

Create new Project in Android Studio,

Open build.gradle and add volley support library,

compile 'com.android.volley:volley:1.0.0'

Maintain volley core objects and request queue is, making them global by creating a singleton class which extends Application object.

create a class named LruBitmapCache.java and paste the below code. This class is required to handle image cache.

Add the AppController.java into the Application tag in manifest.xml to execute this at app launch.and also add internet permission.

Now the setup is done. Below the methods, volley provides to make the HTTP requests.

Making JSON object request

Make a JSON object request where the JSON response will start with object notation ‘{

// Tag used to cancel the request
String tag_json_object = "json_obj_req";
String url = "http://velmm.com/apis/volley_object.json";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.GET,
url, null,
new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.toString());
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsojsonObjectRequestnObjReq, tag_json_object);

Making JSON array request

Make JSON array request where the JSON response starts with array notation ‘[

// Tag used to cancel the request
String tag_json_arry = "json_array_request";
String url = "https://api.androidhive.info/volley/person_array.json";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.toString());
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonArrayRequest, tag_json_arry);

Making String request

StringRequest class will be used to fetch any kind of string data.

// Tag used to cancel the request
String tag_string_request = "string_req";
String url = "http://velmm.com/apis/volley_array.json";
StringRequest stringRequest = new StringRequest(Method.GET,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.toString());
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(stringRequest, tag_string_request);

Making Post Request

To make POST request we have to override getParams() method which should return the list of parameters to send in a key-value format.

// Tag used to cancel the request
String tag_json_object = "json_obj_request";
String url = "http://velmm.com/apis/volley_array.json";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, error.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, response.toString());
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", "Velmm.com");
params.put("password", "*******");
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjectRequest, tag_json_object);

Request prioritization

Making multiple requests at the same time, you can prioritize the requests those you want to be executed first. The priory can be Normal, Low, Immediate and High.

private Priority priority = Priority.HIGH;
StringRequest strReq = new StringRequest(Method.GET,
URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
@Override
public Priority getPriority() {
return priority;
}
};

Making Image request

Volley introduced custom image view element called NetworkImageView to display the images from an URL. This will download images and maintain caches.

Loading image in NetworkImageView

ImageLoader imageLoader = AppController.getInstance().getImageLoader();
imgNetWorkView.setImageUrl(IMG_URL, imageLoader);

Loading image in ImageView

ImageLoader imageLoader = AppController.getInstance().getImageLoader();

imageLoader.get(movieList.get(position).getImageUrl(), new ImageLoader.ImageListener() {

@Override
public void onErrorResponse(VolleyError error) {

}

@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.image.setImageBitmap(response.getBitmap());
}
});

Adding placeholder image and error image

imageLoader.get(IMG_URL, ImageLoader.getImageListener(
imageView, R.drawable.loading, R.drawable.error));

Adding the volley Cache

Volley comes with powerful cache mechanism to maintain request cache. This saves a lot of internet bandwidth and reduces user waiting time.

Loading request from cache

Cache cache = AppController.getInstance().getRequestQueue().getCache();
Cache.Entry entry = cache.get(jsonArrayUrl);
if(entry != null){

try {
String data = new String(entry.data ,"UTF-8");
// Get JSON from the data
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
//Make network call
}

Invalidate cache from volley

This method used to invalidate the cache of the API.That means when the changes added in the API response the cache will update to latest response.

AppController.getInstance().getRequestQueue().getCache().invalidate(url,true);

Turning off cache for volley

You can also turn off the cache for the particular API response using the following code.

// String request
StringRequest stringReq = new StringRequest(....);
// disable cache
stringReq.setShouldCache(false);

Cancel single request in volley

You can cancel the request using the tag name of the request.

String json_array_tag="tag_name";
AppController.getInstance().getRequestQueue().cancelAll("tag_name");

Cancel all request in volley

Use this code to cancel all the request.

AppController.getInstance().getRequestQueue().cancelAll();

Parsing JSON on Android using GSON

In your build.gradle you’ll need to add the dependencies for GSON

compile 'com.google.code.gson:gson:2.4'

Define the Model

Use the SerializedName annotation when the property in the JSON does not exactly match the field of your class.

public class Movie {
@SerializedName("title")
private String title;
@SerializedName("image")
private String imageUrl;
}

Create a new GSON instance in the onCreate method of our Activity.

private Gson gson;
@Override
protected void onCreate(Bundle savedInstanceState) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("M/d/yy hh:mm a");
gson = gsonBuilder.create();
}

This instance of GSON is what we’ll be using to parse the JSON response.

StringRequest strReq = new StringRequest(Request.Method.GET,
jsonArrayUrl, new Response.Listener<String>() {

@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
movieList = Arrays.asList(gson.fromJson(response,Movie[].class));
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.toString());
}
});

Source Available at GitHub

Direct Download Example Here

Read More..

Originally published at velmm.com on January 10, 2018.

Lead Software Engineer @htcindia | @github contributor | Blog writer @howtodoandroid | Quick Learner