My favorites | English | Sign in

Faster JavaScript with Closure Tools New!

Google Base Data API (Labs)

Java Raw Protocol - UpdateExample

UpdateExample shows how to modify your Google Base items.

The previous examples demonstrated how to query the Google Base data API server, both for authenticated and unauthenticated feeds, and how to insert a new item. We will combine this knowledge here in order to demonstrate how to update an existing item.

You will need a Google account email and password properly set up for Google Base in order to run this example.

This example does not introduce any new concepts. Rather, it uses all of the concepts that have already been presented in the previous examples. We will authenticate, insert an item, and then update the inserted item by adding a new label to it.

Contents

  1. Running UpdateExample
  2. Stepping through the UpdateExample code

Running UpdateExample

  1. Obtain a developer key for an "installed application".
  2. Edit UpdateExample.java and fill in the DEVELOPER_KEY static string with your developer key.
  3. private static final String DEVELOPER_KEY = "";
  4. Enter your Google account's email address and your password in the EMAIL and PASSWORD static strings:
  5. private static final String EMAIL = "";
    private static final String PASSWORD = "";
  6. Compile and run the example using your favorite editor, or the command line:
  7. javac com/google/api/gbase/examples/basic/UpdateExample.java
    java com/google/api/gbase/examples/basic/UpdateExample
    

The output will be as follows:

Obtained authorization token: DQAAAGgA...dk3fA5N
Posting item:
<?xml version='1.0'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:g='http://base.google.com/ns/1.0'>
  <category scheme='http://base.google.com/categories/itemtypes' term='Products'/>
  <g:item_type type='text'>Products</g:item_type>
  <title type='text'>My cool car is for sale</title>
  <content type='xhtml'>Light pink, yellow seats.</content>
</entry>

Updating item: http://base.google.com/base/feeds/items/18020038538902937385  
<?xml version='1.0' encoding='UTF-8'?>
<entry>
  <id>http://base.google.com/base/feeds/items/18020038538902937385</id>
  <updated>2006-08-23T16:20:21.601Z</updated>
  <category scheme="http://base.google.com/categories/itemtypes" term="Products"/>
  <title type="text">My cool car is for sale</title>
  <content type="xhtml">Light pink, yellow seats.</content>
  <link rel="self" type="application/atom+xml" href="http://base.google.com/base/feeds/items/18020038538902937385"/>
  <link rel="edit" type="application/atom+xml" href="http://base.google.com/base/feeds/items/18020038538902937385"/>
  <g:item_type type="text">Products</g:item_type>
</entry>

Stepping through the UpdateExample code

The main method of UpdateExample.java provides a good outline of the example's structure.

public static void main(String[] args) throws MalformedURLException, IOException {
  UpdateExample updateExample = new UpdateExample();
  String token = updateExample.authenticate();
  System.out.println("Obtained authorization token: " + token);
  
  System.out.println("Posting item:\n" + DATA_ITEM);
  String itemUrl = updateExample.extractItemUrlFromResponse(
    updateExample.postItem(token));
    
  System.out.println("Updating item: " + itemUrl);
  String updateResponse = updateExample.updateItem(token, itemUrl);
  System.out.println(updateResponse);
}

As in the previous examples, we begin by authenticating and obtaining an authorization token using the authenticate() method:

String token = updateExample.authenticate();

We then insert DATA_ITEM using postItem():

String itemUrl = updateExample.extractItemUrlFromResponse(
        updateExample.postItem(token));

In the line above, we pass the output of the post operation to extractItemUrlFromResponse() rather than dumping it out to the console. This extracts the inserted item's ID:

<id>http://base.google.com/base/feeds/items/18020038538902937385</id>

We assume that the item's ID is surrounded by the first <id></id> tags. This is true for the Google Base data API servers, but it's not enforced by the Atom protocol. Refer to the way the title gets parsed in Query Example 2 for a better way to parse the item's title.

Once DATA_ITEM is successfully inserted, we replace it with NEW_DATA_ITEM using updateItem(). Updating an item is very similar to inserting a new one; in fact, it is so similar that both operations can be performed by the same method: makeHttpRequest(). makeHttpRequest() receives as parameters the authorization token, the url to connect to, the item to be inserted or posted, the HTTP method to use (this will be POST for an insert, and PUT for an update) and the response code to expect in case of a successful operation (HTTP_CREATED for an insert, HTTP_OK for an update). Thus, postItem() will contain a simple invocation to makeHttpRequest():

public String postItem(String token) throws IOException {
  return makeHttpRequest(token, ITEMS_FEED, DATA_ITEM, "POST", HttpURLConnection.HTTP_CREATED);
}  

Similarly, updateItem() invokes makeHttpRequest() with slightly different parameters:

public String updateItem(String token, String itemUrl) throws MalformedURLException, IOException {
  return makeHttpRequest(token, itemUrl, NEW_DATA_ITEM, "PUT", HttpURLConnection.HTTP_OK);
}