Sunday, September 27, 2015

Android Networking & Libraries – A closer look



When it comes to networking, there are many libraries in android which makes the calls faster, smoother and easier. I was in a research of finding the best or the best combination for a variety of projects/requirements. Here is a summary of my observations.

Why third party libraries over available system APIs?
·         Parallel execution of api calls
·         Most libraries has an ability to cancel/revoke networks calls.
·         Async interface to avoid main/UI thread blocking.
·         Retry policies
·         Json serialization
·         support SPDY, HTTP/2
·         Reuse of existing socket connections ( connection pooling )
·         Local response caching
·         Large bitmap handling/loading/ transformation

Design Patterns
According to the presentation by Virgis in 2010 Google I/O here , the networking design patterns to implement a rest client is
·         Service API
·         Content Provider API
·         Content Provider API and SyncAdapter
The gist is, to do work on a worker thread in which the life cycle is managed by service class. In case of network request which are not related to activity a sync adapter can be used.

Inside Android HTTP -  HttpURLConnection vs Apache HTTP
According to the official blog post , “Prior to Froyo, HttpURLConnection had some frustrating bugs. In particular, calling close() on a readable InputStream could poison the connection pool.” But due to the large size of APIs google was not ready to work on improving ApacheHttp client. For Gingerbread and above versions HttpUrlConnection was recommended and due to annoying bugs prior to Froyo , Apache HTTP client was the perfect choose.
Most developers used a legacy work around for the android http connections based on the API versions.
if (Build.VERSION.SDK_INT >= 9) {
HttpUrlConnection
                 }  else {
        // Prior to Gingerbread, HttpUrlConnection was unreliable.
       Apache - AndroidHttpClient.
    }

Okhttp by Square.inc
Square implemented OkHttp using the popular HttpUrlConnection and Apache client interfaces. Okhttp Engine includes HttpUriConnection and okApacheclient. According to this tweet by Jake Wharton , Okhttp became the engine that powers HttpUrlConnection in android 4.4.

Volley by Google
In Google I/O 2013, Google introduced Volley.
Everything in Volley sits on a top of HttpUrlConnection. Volley has special abstractions ImageRequest and JsonObjectRequest, which helps to automatically convert HTTP payload.
In certain cases OkHttp will perform better by utilizing more threads as volley has a hardcoded thread pool size (reference  ).
It is possible to use okhttp with volley using HttpStack implementation which uses OkHttp's request/response API instead of HttpURLConnection. (Reference)

Retrofit by Square.inc
Retrofit is one of the most elegant libraries around for created REST clients. An API can be implemented as an interface class, which contains java annotations to describe how a default RestAdapter will communicate with the API.
Retrofit provides a natural bridge between Java code and REST interfaces. It quicky turns HTTP API into Java interface and generates a self-documenting implementation
In addition, Retrofit also supports conversion to JSON, XML, protocol buffers.

Picasso by Square,  Fresco by Facebook and Glide
Picasso, on the other hand, provides an HTTP library for image-specific tasks.
Both Picasso and Retrofit configurable to use OkHttpClient as default HTTP client. However, we can specify a HttpURLConnection-based client if we needs. Glide is somewhat similar to Picasso. It provides additional features, such as GIF animations, thumbnail generation, and still videos.
Facebook recently open-sourced it's own image library Fresco that it uses in its Android client. One of key features that stands out is that Fresco's custom memory allocation strategy for bitmaps to avoid costly garbage collector operations in JVM heap and JVM per-app memory limits.

The HTTP transport component is interchangeable for higher level libraries ( Retrofit/picasso/Fresco/Glide/Volley ). It is possible to choose between plain HttpUrlConnection or the latest OkHttpClient.