Transmitting Network Data Using Volley in Android

In my last article, we learn about    Networking in Android. Now we see implementation of Transmitting Network Data Using Volley in 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.

At a high level, you use Volley by creating a RequestQueue and passing it Request objects. The RequestQueue manages worker threads for running the network operations, reading from and writing to the cache, and parsing responses. Requests do the parsing of raw responses and Volley takes care of dispatching the parsed response back to the main thread for delivery.

This Concepts describes how to send a request using the Volley.newRequestQueue convenience method, which sets up a RequestQueue for you. See the next lesson, setting Up a RequestQueue, for information on how to set up a RequestQueue yourself.

Volley offers the following benefits:

·         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.

Volley core

When we use Volley there are some classes that play an important role and most probably they are the classes we will use more often. These classes are:

·         RequestQueue

·         Request

·         Response

·         Some useful class that simplify our work (toolbox package)


Here is an example which help you to use Volley in your android application.


1.      Create an Android project and API level must be greater than 3.

2.      Add volley.jar file in your Android Library File.

3.      My activity_main.xml file has following code


<ScrollView xmlns:android=""








    tools:context=".MainActivity" >






        android:layout_height="wrap_content" />




4.      Add following code in MainActivity.class


package com.example.androidvolleysample;









import android.os.Bundle;



import android.view.Menu;

import android.widget.TextView;


public class MainActivity extends Activity {

     TextView textView;

     RequestQueue mRequestQueue;

     private ProgressDialog pDialog;

     public static final String myUrl = "";




     protected void onCreate(Bundle savedInstanceState) {



      pDialog = new ProgressDialog(this);




      textView = (TextView) findViewById(; 


      // Instantiate the RequestQueue.

      mRequestQueue = Volley.newRequestQueue(this);


      // Show process dialog



      // Request a string response from the provided URL.

      StringRequest strReq = new StringRequest(Method.GET, myUrl,

        new Response.Listener<String>() {



          public void onResponse(String response) {

            // Display the first 1000 characters of the response

            // string.

            textView.setText(response.substring(0, 1000));

            // hide process dialog



         , new Response.ErrorListener() {



         public void onErrorResponse(VolleyError error) {

         // hide process dialog





       if (mRequestQueue != null)

          // Add the request to the RequestQueue.




     private void showProgressDialog() {

           if (!pDialog.isShowing())





     private void hideProgressDialog() {

           if (pDialog.isShowing())






    public boolean onCreateOptionsMenu(Menu menu) {

     // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(, menu);

           return true;





Now run your application

Leave Comment