Movatterモバイル変換


[0]ホーム

URL:


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 theGoogle 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 thesetup page, and pay special attention to the Android instructions forProGuard. Using ProGuard or a similar tool to remove unused code and compress it is critical for minimizing application size. For example, for thetasks-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 aNoClassDefFoundError error at runtime.

Data models

JSON

You have a choice of threepluggable streaming JSON libraries. Options includeJacksonFactory for maximum efficiency, orAndroidJsonFactory for the smallest application size on Honeycomb (SDK 3.0) or higher.

XML (@Beta)

TheXML 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 theAccountManager 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 theNetHttpTransport class. This class isbased onHttpURLConnection, which is built into the Android SDK and is found in all Java SDKs.

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

If your Android application needs to work with all Android SDKs, callAndroidHttp.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 useLevel.CONFIG, the value of the Authorization header is not shown. To show the Authorization header, useLevel.ALL.

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

adbshellsetproplog.tag.HttpTransportDEBUG

[8]ページ先頭

©2009-2025 Movatter.jp