onsdag 7 november 2012

Introducing DroidNetworking - A network library for Android

Most Android apps need to use HTTP to send and receive data. There are many options for network communication in Android. Google recommends using the Apache Http Client for Eclair (2.1) and Froyo (2.2). For Gingerbread (2.3) and newer the HttpURLConnection is recommended. However response caching was not introduced in the HttpURLConnection until Ice Cream Sandwich (4.0) was released.

DroidNetworking is a network library built on top of the Apache Http Client. It has support for response caching, authentication, HTTP and HTTPS and many other features. Best of all, DroidNetworking can be used on Eclair (2.1) and newer. It has a simple API which reduces the amount of code needed for network communication.

Basic example

DroidNetworking supports both synchronous and asynchronous requests. The example below shows a simple asynchronous GET request that will be executed in a background thread.

Using the input stream directly in combination with a JSON stream parser will improve performance and reduce memory consumption, which is important when working with large responses. The parser must be set before executing the request.

Response cache

DroidNetworking includes a HTTP response cache with support for both memory and disk cache. By default the cache is disabled but it can be enabled by calling:

NetworkEngine.getInstance().setUseCache(true);

The default memory cache size is 2 MB and the default disk cache size is 10 MB. These sizes can be customized by calling: