articles

Home / DeveloperSection / Articles / Transmitting Network Data Using Volley in Android

Transmitting Network Data Using Volley in Android

Manoj Pandey6467 20-Feb-2015

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="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
          android:id="@+id/tvTitle"
          android:textSize="20sp"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />

</ScrollView>

4. Add following code in MainActivity.class  

package com.example.androidvolleysample; 

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

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
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 = "http://www.mindstick.com";

     //
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      pDialog = new ProgressDialog(this);
      pDialog.setMessage("Loading...");
      pDialog.setCancelable(false);
      setContentView(R.layout.activity_main);
      textView = (TextView) findViewById(R.id.tvTitle);

      // Instantiate the RequestQueue.
      mRequestQueue = Volley.newRequestQueue(this);

      // Show process dialog
      showProgressDialog();

      // Request a string response from the provided URL.
      StringRequest strReq = new StringRequest(Method.GET, myUrl,
        new Response.Listener<String>() {

          @Override
          public void onResponse(String response) {
            // Display the first 1000 characters of the response
            // string.
            textView.setText(response.substring(0, 1000));
            // hide process dialog
            hideProgressDialog();
           }
         , new Response.ErrorListener() {

         @Override
         public void onErrorResponse(VolleyError error) {
         // hide process dialog
         hideProgressDialog();

        }
       });
       if (mRequestQueue != null)
          // Add the request to the RequestQueue.
          mRequestQueue.add(strReq);
     }

     private void showProgressDialog() {
           if (!pDialog.isShowing())
                pDialog.show();

     }

     private void hideProgressDialog() {
           if (pDialog.isShowing())
                pDialog.hide();

     }

     @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;
     }

}

 Now run your application

Transmitting Network Data Using Volley in Android


Updated 07-Sep-2019

Leave Comment

Comments

Liked By