My favorites | English | Sign in

Faster apps faster - GWT 2.0 with Speed Tracer New!

Google Base Data API (Labs)

Java Raw Protocol - QueryExample1

QueryExample1 queries the Google Base data API and displays the result. It is a simple Java application that runs from the command line.

QueryExample1 performs an unauthenticated query on the public snippets feed (/feeds/snippets) and dumps the unformatted query response to the console.

Contents

  1. Running QueryExample1
  2. Stepping through the QueryExample1 code

Running QueryExample1

  1. Obtain a developer key for an "installed application".
  2. Edit QueryExample1.java and fill in the DEVELOPER_KEY static string with your developer key.
  3. private static final String DEVELOPER_KEY = "";
  4. Compile and run the example using your favorite editor, or using the command line:
  5. javac com/google/api/gbase/examples/basic/QueryExample1.java
    java com/google/api/gbase/examples/basic/QueryExample1
    

The output (formatted here for readability) will be as follows:

<feed>
  <id>http:base.google.com/base/feeds/snippets</id>
  <updated>2006-08-22T14:14:11.984Z</updated>  
  <title type="text">Items matching query: cars [item type : products]</title>
  <link rel="alternate" type="text/html" href="http://base.google.com"/>
  <link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://base.google.com/base/feeds/snippets"/>
  <link rel="self" type="application/atom+xml" href="http://base.google.com/base/feeds/snippets?key=ABQ...9P2Y4A&bq=cars+%5Bitem+type+%3A+products%5D"/>
  <link rel="next" type="application/atom+xml" href="http://base.google.com/base/feeds/snippets?start-index=26&max-results=25&key=ABQ...9P2Y4A&bq=cars+%5Bitem+type+%3A+products%5D"/>
  <generator version="1.0" uri="http://base.google.com">GoogleBase</generator>
  <openSearch:totalResults>278120</openSearch:totalResults>
  <openSearch:itemsPerPage>25</openSearch:itemsPerPage>
    <entry>
      <id>http://base.google.com/base/feeds/snippets/10062394959501653657</id>
      <published>2006-06-30T21:45:12.000Z</published>
      <updated>2006-07-28T00:58:14.000Z</updated>  
        ...

Stepping through the QueryExample1 code

First, we define the URL of the feed we are connecting to, as well as the query that we are going to run.

private static final String SNIPPETS_FEED = "http://base.google.com/base/feeds/snippets";
private static final String QUERY = "cars [item type : products]";

Feel free to change the query to a more relevant or interesting one. Take a look at the Google Base Query Language description if you need some inspiration.

The Google Base data API server requires a developer key to connect to any of the feeds. If you haven't already obtained one, you can obtain one. You only need to do this once; once you have a developer's key, it can be used for any request.

If you already have a Maps API key, just use that one (caveat: if the key was generated for a URL which also has path components, it will not work for Google Base data API authsub applications).

Insert your developer key in the DEVELOPER_KEY attribute.

The displayItems() method creates a java.net.URL instance, which connects to the snippets feed. It passes the URL-encoded query as the bq (Base Query) parameter, and the developer key as the key parameter:

URL url = new URL(SNIPPETS_FEED + "?bq=" + 
  URLEncoder.encode(QUERY, "UTF-8") + "&key=" + DEVELOPER_KEY);

After opening a connection on the snippets feed, we grab the connection's input stream and dump its content to the output, character by character.

HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = httpConnection.getInputStream();


int ch;
  while ((ch=inputStream.read()) > 0) {
    System.out.print((char)ch);
  }

In the main method, we create an instance of QueryExample1, and invoke its displayItems() method.

public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
  new QueryExample1().displayItems();
}

Go to QueryExample2