An Introduction
Volley is a networking toolkit built by +Ficus Kirkpatrick and team(?) for the Google Play Store app. It's well architected, easy to understand and extend, and best of all, it's built on top of a fluent interface(Yes, I love fluent interfaces.. Who doesn't?).
If you haven't already watched the video, you really should. Personally, it was one of my favourite talks from this year's Google I/O sessions.
Off the top of my head, here's a quick list of some of its best features
If you haven't already watched the video, you really should. Personally, it was one of my favourite talks from this year's Google I/O sessions.
Off the top of my head, here's a quick list of some of its best features
- Concurrent
- Works well with the Activity/Fragment lifecycle(this is a HUGE plus, in my opinion)
- Handles Image Loading
- Switches between the Apache HttpClient and HttpUrlConnection automatically based on the device OS(for those who don't know, Android 2.2 has bugs with the HttpUrlConnection)
- Has a Disk-based cache for the network responses, which returns the cached response based on the HTTP Response headers
- Has an awesome request cancellation policy
Setup
The first thing you need to do is to create an instance of RequestQueue and ImageLoader(if needed). I find the best place to do this is in the onCreate() of a custom Application Class and then hand them over to the Activities and Fragments as required.
The BitmapLruCache is the same as the one mentioned in the Android developer guide for Caching Bitmaps. It just needs to implement the ImageCache interface of ImageLoader.
Usage
Talking with APIs
Now, we get to the fun part. Before we make requests, we should initialize two Listeners, one for a successful response and one for an error response.
Once you've done this, making an API call is as simple as
Volley will pick up requests from the Queue and execute them. Your Listener methods will get called automatically when the request finishes. You can queue up as many requests as you like. In case your Activity/Fragment gets stopped before any queued requests complete, Volley can cancel any pending requests from that Activity/Fragment and also prevent responses getting delivered to the stopped Activity/Fragment. All you need to do is override the onStop() method in your Activity/Fragment.
Voila! No more null checks in your Listener code!
Image Loading
Image Loading was a right pain until now, what with caching and rescaling Bitmaps and recycling of views in AdapterViews. Volley takes care of all that. You can use Volley's ImageRequest, but you'll still have to cancel it when recycling Views in AdapterViews. What you can do instead is use their NetworkImageView widget in your layouts.
All you need to do in your code then is
That's all for today. Hope you liked it! Part 2 will talk about how we modified and extended Volley for our Networking Framework. For a sneak peek at some of the modifications, check out Enhanced Volley, which is my fork of Volley including some of my enhancements. I plan to keep it up to date with the enhancements and fixes made to Volley as well.
Happy coding!!