Slide 1

Slide 1 text

Introduction to Volley Paresh Mayani

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Volley Simultaneous firing of a number of missiles

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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.

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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)

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Thank You +PareshMayani @pareshmayani TechnoTalkative.com