Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Volley

Introduction to Volley

Volley is an open-source library written by Google (Android team), released during Google I/O 2013.

Paresh Mayani

May 10, 2014
Tweet

More Decks by Paresh Mayani

Other Decks in Technology

Transcript

  1. Volley library o An open-source library written by Google (Android

    team) o Volley isn’t an image loading library - it’s an asynchronous networking library o Introduced by Ficus Kirkpatrick at Google I/O 2013 o It’s not yet added to Android API
  2. Advantages o Standalone library o A high level API to

    make asynchronous RESTful HTTP requests o Schedules all your HTTP requests running them parallel in background threads and manages those threads. o Good asynchronous performance o Provides powerful cancellation request API. o Comes with inbuilt JSON parsing the response. o Handles memory errors well o Retry policy for timeout, certain ERROR codes as Internal Server error.
  3. Let’s use Volley Step 1: Create a RequestQueue Instance Step

    2: Create a JSONObjectRequest with response and error listener. Step 3: Add your request into the RequestQueue.
  4. Step 2 To make asynchronous HTTP requests, there are 3

    utility classes available: JsonObjectRequest - To send and receive JSON Object from the Server JsonArrayRequest - To receive JSON Array from the Server StringRequest - To retrieve response body as String (ideally if you intend to parse the response by yourself)
  5. Step 2 Create a JSONObjectRequest with response and error listener.

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // TODO Auto-generated method stub } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO Auto-generated method stub } });
  6. Image loading using Volley Step 1: Get NetworkImageView in XML

    layout Step 2: Create ImageLoader object Step 3: Load Image using setImageUrl(url, imageLoader)
  7. Image loading using Volley <com.android.volley.toolbox.NetworkImageView android:id="@+id/imgDemo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="centerCrop"/> ImageLoader.ImageCache

    imageCache = new BitmapLruCache(); ImageLoader imageLoader = new ImageLoader(Volley.newRequestQueue(context), imageCache); NetworkImageView imgDemo = (NetworkImageView) findViewById(R.id.imgDemo); imgDemo.setImageUrl(url, imageLoader); imgDemo.setDefaultImageResId(..); imgDemo.setErrorImageResId(..);