Network Programming in Android

APIs provided by Google

  • AndroidHttpClient
  • HttpUrlConnection

AndroidHttpClient

  • Useful for building web browsers
  • Allows for multithreaded connection pool
  • Highly configurable
  • Best choice when developing for Eclair and Froyo 
  • The android team no longers works on it

HttpUrlConnection

  • General-purpose, lightweight HTTP client

  • Automatically adds GZip compression

  • Highly configurable

  • From ICS, it has response caching 

  • Also supports conditionally cached responses

  • For Gingerbread and above this should be the better choice

Demo 

okHttp

  • Started as a fork of HttpUrlConnection
  • HTTP/2 and SPDY support allows all requests to the same host to share a socket.
  • Connection pooling reduces request latency (if SPDY isn’t available).
  • it will silently recover from common connection problems
    • Attempt alternate address

    • Follow up requests

    • Retrying Requests

  • Simultaneous Requests

  • OkHttp doesn't currently offer asynchronous APIs to receive a response body in parts.

okHttp

Volley

  • Boilerplate
  • Caching Solutions
  • Simple Request Context Management
  • Scheduling of network requests

  • Volley is not suitable for large download or streaming operations, since Volley holds all responses in memory during parsing

Volley vs Ok Http

OkHttp is an modern, fast and efficient Http client which supports HTTP/2 and SPDY and acts as the transport layer

Volley is a REST client that makes easy common networking tasks. 

Demo

Retrofit

  • Retrofit turns your REST API into a Java interface.

  •  The request method and relative URL are added with an annotation

  • Adding a return type to a method will make it synchronous, while adding a Callback will allow it to finish asynchronously with success or failure.

Picasso

  • Complex image transformations with minimal memory use.
  •  Automatic memory and disk caching.

  • Adapter re-use is automatically detected and the previous download canceled.A request will be retried three times before the error placeholder is shown.

  • Resources, assets, files, content providers are all supported as image sources.

Picasso.with(context)
    .load(url)
    .placeholder(R.drawable.user_placeholder)
    .error(R.drawable.user_placeholder_error)
    .into(imageView);

Fresco

  • Uses Ashmem Heap
  •  Purgeable Bitmaps

  • Image loader pipeline

  • Support for progressive Images 

  • Supports animations

  • Optionally we can use okHttp as the transport Layer

Thank You

Http Networking in Android

By Abhik Mitra

Http Networking in Android

  • 1,678