Google API for Java 示例代码,api示例代码,Google API f
分享于 点击 31192 次 点评:83
Google API for Java 示例代码,api示例代码,Google API f
Google API for Java是由Google开发的一个Java客户端类库,用于访问其提供的各种基于HTTP的服务。具有灵活、高效、强大等特点。这是访问基于REST或JSON-RPC的Google API推荐类库。
BigQuerySample.java
import com.google.api.client.googleapis.*;import com.google.api.client.googleapis.auth.clientlogin.*;import com.google.api.client.googleapis.json.*;import com.google.api.client.http.*;import java.io.*;public class BigQuerySample { public static void main(String[] args) throws IOException { HttpTransport transport = GoogleTransport.create(); transport.addParser(new JsonCParser()); try { // authenticate with ClientLogin ClientLogin authenticator = new ClientLogin(); authenticator.authTokenType = "ndev"; authenticator.username = "..."; authenticator.password = "..."; authenticator.authenticate().setAuthorizationHeader(transport); // make query request HttpRequest request = transport.buildGetRequest(); request.setUrl("https://www.googleapis.com/bigquery/v1/query"); request.url.put( "q", "select count(*) from [bigquery/samples/shakespeare];"); System.out.println(request.execute().parseAsString()); } catch (HttpResponseException e) { System.err.println(e.response.parseAsString()); throw e; } }}
YouTubeSample.java
import com.google.api.client.googleapis.json.*;import com.google.api.client.http.*;import com.google.api.client.util.*;import java.io.*;import java.util.*;public class YouTubeSample { public static class VideoFeed { @Key List<Video> items; } public static class Video { @Key String title; @Key String description; @Key Player player; } public static class Player { @Key("default") String defaultUrl; } public static class YouTubeUrl extends GenericUrl { @Key final String alt = "jsonc"; @Key String author; @Key("max-results") Integer maxResults; YouTubeUrl(String url) { super(url); } } public static void main(String[] args) throws IOException { // setup up the HTTP transport HttpTransport transport = new HttpTransport(); transport.defaultHeaders.put("GData-Version", "2"); transport.addParser(new JsonCParser()); // build the HTTP GET request and URL HttpRequest request = transport.buildGetRequest(); YouTubeUrl url = new YouTubeUrl("https://gdata.youtube.com/feeds/api/videos"); url.author = "searchstories"; url.maxResults = 2; request.url = url; // execute the request and the parse video feed VideoFeed feed = request.execute().parseAs(VideoFeed.class); for (Video video : feed.items) { System.out.println(); System.out.println("Video title: " + video.title); System.out.println("Description: " + video.description); System.out.println("Play URL: " + video.player.defaultUrl); } }}
PicasaAndroidSample.java
public final class PicasaAndroidSample extends ListActivity {... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);... gotAccount(false); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_ACCOUNTS: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Select a Google account"); final AccountManager manager = AccountManager.get(this); final Account[] accounts = manager.getAccountsByType("com.google"); final int size = accounts.length; String[] names = new String[size]; for (int i = 0; i < size; i++) { names[i] = accounts[i].name; } builder.setItems(names, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { gotAccount(manager, accounts[which]); } }); return builder.create(); } return null; } private void gotAccount(boolean tokenExpired) { SharedPreferences settings = getSharedPreferences(PREF, 0); String accountName = settings.getString("accountName", null); if (accountName != null) { AccountManager manager = AccountManager.get(this); Account[] accounts = manager.getAccountsByType("com.google"); int size = accounts.length; for (int i = 0; i < size; i++) { Account account = accounts[i]; if (accountName.equals(account.name)) { if (tokenExpired) { manager.invalidateAuthToken("com.google", this.authToken); } gotAccount(manager, account); return; } } } showDialog(DIALOG_ACCOUNTS); } private void gotAccount(final AccountManager manager, final Account account) { SharedPreferences settings = getSharedPreferences(PREF, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("accountName", account.name); editor.commit(); new Thread() { @Override public void run() { try { final Bundle bundle = manager.getAuthToken(account, AUTH_TOKEN_TYPE, true, null, null) .getResult(); runOnUiThread(new Runnable() { public void run() { try { if (bundle.containsKey(AccountManager.KEY_INTENT)) { Intent intent = bundle.getParcelable(AccountManager.KEY_INTENT); int flags = intent.getFlags(); flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK; intent.setFlags(flags); startActivityForResult(intent, REQUEST_AUTHENTICATE); } else if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) { authenticatedClientLogin( bundle.getString(AccountManager.KEY_AUTHTOKEN)); } } catch (Exception e) { handleException(e); } } }); } catch (Exception e) { handleException(e); } } }.start(); } @Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUEST_AUTHENTICATE: if (resultCode == RESULT_OK) { gotAccount(false); } else { showDialog(DIALOG_ACCOUNTS); } break; } } private void authenticatedClientLogin(String authToken) { this.authToken = authToken; ((GoogleHeaders) transport.defaultHeaders).setGoogleLogin(authToken); authenticated(); }}
用户点评