Google API for Java 示例代码

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;
  }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwsgxf.html