Google HTTP Client for Java

Overview
Setup Instructions
Component Modules
Android
Google App Engine
HTTP Transport
JSON
Exponential Backoff
Unit Testing
Support

Using the Google HTTP Client Library for Java on Android

If you are developing for Android and the Google API you want to use is included in the Google Play Services library, use that library for the best performance and experience. If the Google API you want to use with Android is not part of the Google Play Services library, you can use the Google HTTP Client Library for Java, which supports Android 1.5 (or higher), and which is described here.

Beta

Android support for the Google HTTP Client Library for Java is @Beta.

Installation

Follow the download instructions on the setup page, and pay special attention to the Android instructions for ProGuard. Using ProGuard or a similar tool to remove unused code and compress it is critical for minimizing application size. For example, for the tasks-android-sample, ProGuard reduces the application size ~88%, from 777KB to 93KB.

Note that ProGuard only runs when preparing your application for release; it does not run when preparing it for debugging, to make it easier to develop. However, be sure to test your application in release mode, because if ProGuard is misconfigured it can cause problems that are sometimes a challenge to debug.

Warning: For Android, you MUST place the jar files in a directory named “libs” so that the APK packager can find them. Otherwise, you will get a NoClassDefFoundError error at runtime.

Data models

JSON

You have a choice of three pluggable streaming JSON libraries. Options include JacksonFactory for maximum efficiency, or AndroidJsonFactory for the smallest application size on Honeycomb (SDK 3.0) or higher.

XML (@Beta)

The XML data model (@Beta) is optimized for efficient memory usage that minimizes parsing and serialization time. Only the fields you need are actually parsed when processing an XML response.

Android already has an efficient, native, built-in XML full parser implementation, so no separate library is needed or advised.

Authentication

The best practice on Android (since the 2.1 SDK) is to use the AccountManager class (@Beta) for centralized identity management and credential token storage. We recommend against using your own solution for storing user identities and credentials.

For details about using the AccountManager with the HTTP service that you need, read the documentation for that service.

HTTP transport

If your application is targeted at Android 2.3 (Gingerbread) or higher, use the NetHttpTransport class. This class isbased on HttpURLConnection, which is built into the Android SDK and is found in all Java SDKs.

In prior Android SDKs, however, the implementation of HttpURLConnection was buggy, and the Apache HTTP client was preferred. For those SDKs, use the ApacheHttpTransport class.

If your Android application needs to work with all Android SDKs, call AndroidHttp.newCompatibleTransport() (@Beta), and it will decide which of the two HTTP transport classes to use, based on the Android SDK level.

Logging

To enable logging of HTTP requests and responses, including URL, headers, and content:

Logger.getLogger(HttpTransport.class.getName()).setLevel(Level.CONFIG);

When you use Level.CONFIG, the value of the Authorization header is not shown. To show the Authorization header, use Level.ALL.

Furthermore, you must enable logging on your device as follows:

adb shell setprop log.tag.HttpTransport DEBUG