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.
To access other Google APIs, use the Google APIs Client Library for Java’s Android-specific helper classes, which are well-integrated with Android AccountManager.
For example:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Google Accounts
credential =
GoogleAccountCredential.usingOAuth2(this, Collections.singleton(TasksScopes.TASKS));
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
// Tasks client
service =
new com.google.api.services.tasks.Tasks.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("Google-TasksAndroidSample/1.0").build();
}
Begin by reading the Android development instructions for the Google HTTP Client Library for Java.
As described in the Android development instructions, the
best practice on Android is to use the AccountManager
class
(@Beta
) for centralized identity management and credential token storage.
For information about the OAuth 2.0 flow, see the OAuth 2.0 instructions for Android.
Google APIs support a partial-response protocol that allows you to specify which fields are returned to you in the HTTP response. This can significantly reduce the size of the response, thereby reducing network usage, parsing response time, and memory usage. It works with both JSON and XML.
The following snippet of code drawn from the Google Drive API Quickstart
demonstrates how to use the partial-response protocol. The setFields
method
identifies the fields you want returned:
// Print the names and IDs for up to 10 files.
FileList result = service.files().list()
.setPageSize(10)
.setFields("nextPageToken, files(id, name)")
.execute();
List<File> files = result.getFiles();
if (files == null || files.isEmpty()) {
System.out.println("No files found.");
} else {
System.out.println("Files:");
for (File file : files) {
System.out.printf("%s (%s)\n", file.getName(), file.getId());
}
}