Transmitting Network Data Using Volley In Android

Posted By : Sapna Sharma | 30-Sep-2015

Transmitting Network Data Using Volley In Android

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. This networking library was introduced to make networking calls much easier, faster without writing tons of code. By default all the volley network calls works asynchronously, so we don’t have to worry about using asynctask anymore.

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

  • Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.

  • Debugging and tracing tools.

 

Add the Volley Library

When you are done with project creation, add Volley to the list of dependencies via Gradle.

dependencies {  
  // ...
  compile 'com.mcxiaoke.volley:library:1.0.+'
}

 

Create a RequestQueue

With Volley in our project, we can start using it to download remote content. Volley uses the concept of RequestQueue to manage requests for content and download them. We can create a global request queue for our application in a customer Application subclass.

Create a new VolleyApplication class with a RequestQueue field. We'll initialise the RequestQueue in the application's onCreate method, and keep a static instance of VolleyApplication to let us access the queue from anywhere in our app:

 

class VolleyApplication extends Application {

  private static VolleyApplication sInstance;

  private RequestQueue mRequestQueue;

  @Override
  public void onCreate() {
    super.onCreate();

    mRequestQueue = Volley.newRequestQueue(this);

    sInstance = this;
  }

  public synchronized static VolleyApplication getInstance() {
    return sInstance;
  }

  public RequestQueue getRequestQueue() {
    return mRequestQueue;
  }
}
 

 

Be sure to update your AndroidManifest.xml to specify VolleyApplication as the application's class. We'll also need to request the INTERNET permission:


  

  
    ...
  
  
 

 

Download a JSON Feed

Now we can create a request and pass it to Volley.

 

 public class MainActivity extends ActionBarActivity {  
  private TextView mTextView;

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

      mTextView = (TextView) findViewById(R.id.text1);

      JsonObjectRequest request = new JsonObjectRequest("http://cblunt.github.io/blog-android-volley/response.json", null,
                                                        new Response.Listener() {

                                                            @Override
                                                            public void onResponse(JSONObject response) {

                                                                mTextView.setText(response.toString());
                                                            }
                                                        },

                                                        new Response.ErrorListener() {

                                                            @Override
                                                            public void onErrorResponse(VolleyError error) {
                                                                mTextView.setText(error.toString());
                                                            }
                                                        }
      );
      VolleyApplication.getInstance().getRequestQueue().add(request);
  }

 

 

Finally, with the request created, we can add it to the app's RequestQueue for Volley to handle. Run your app to see the returned JSON response.

Thanks

About Author

Author Image
Sapna Sharma

Sapna is a bright Android Apps developer using Titanium framework. Sapna likes music and helping needy people.

Request for Proposal

Name is required

Comment is required

Sending message..