If you are developing for Android and the Google API you want to use is includedin theGoogle Play Services library, use that library for thebest 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 withAndroid AccountManager.
For example:
@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);// Google Accountscredential=GoogleAccountCredential.usingOAuth2(this,Collections.singleton(TasksScopes.TASKS));SharedPreferencessettings=getPreferences(Context.MODE_PRIVATE);credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME,null));// Tasks clientservice=newcom.google.api.services.tasks.Tasks.Builder(httpTransport,jsonFactory,credential).setApplicationName("Google-TasksAndroidSample/1.0").build();}
Begin by reading theAndroid development instructions forthe Google HTTP Client Library for Java.
As described in theAndroid development instructions, thebest practice on Android is to use theAccountManager
class(@Beta
) for centralized identity management and credential token storage.
For information about the OAuth 2.0 flow, see theOAuth 2.0 instructions for Android.
Google APIs support a partial-response protocol that allows you to specify whichfields are returned to you in the HTTP response. This can significantly reducethe 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 theGoogle Drive API Quickstart demonstrates how to use the partial-response protocol. ThesetFields
methodidentifies the fields you want returned:
// Print the names and IDs for up to 10 files.FileListresult=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(Filefile:files){System.out.printf("%s (%s)\n",file.getName(),file.getId());}}