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. Introduction to Volley
    Paresh Mayani

    View Slide

  2. Paresh Mayani
    Sr. Software Engineer @ InfoStretch, India
    Manager, GDG Ahmedabad

    View Slide

  3. Writing
    Http code
    every
    time?
    {Less code}
    {Less Mistakes}
    {Faster Development}

    View Slide

  4. Volley
    Simultaneous firing of a number of missiles

    View Slide

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

    View Slide

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

    View Slide

  7. Disadvantages
    o It’s not well documented
    o No active contributors
    o Advanced HTTP features need custom code

    View Slide

  8. Get Volley
    $ git clone https://android.googlesource.com/platform/frameworks/volley
    $ cd volley
    $ android update project -p .
    $ ant jar

    View Slide

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

    View Slide

  10. Step 1
    Create a RequestQueue Instance
    RequestQueue queue = Volley.newRequestQueue(this);

    View Slide

  11. 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)

    View Slide

  12. Step 2
    Create a JSONObjectRequest with response and error listener.
    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null,
    new Response.Listener() {
    @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
    }
    });

    View Slide

  13. Step 3
    Add your request into the RequestQueue
    queue.add(jsObjRequest);

    View Slide

  14. Image loading using Volley
    Step 1: Get NetworkImageView in XML layout
    Step 2: Create ImageLoader object
    Step 3: Load Image using setImageUrl(url, imageLoader)

    View Slide

  15. Image loading using Volley
    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(..);

    View Slide

  16. References/links
    o https://android.googlesource.com/platform/frameworks/volley/
    o https://developers.google.com/events/io/sessions/325304728

    View Slide

  17. Thank You
    +PareshMayani @pareshmayani
    TechnoTalkative.com

    View Slide