0 Replies - 20436 Views - Last Post: 31 August 2010 - 01:55 AM Rate Topic: -----

#1 b0ng01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

[Java]Google Data Java Client API

Posted 31 August 2010 - 01:55 AM


The Google Data Protocol is an Application Programming Interface released by Google Inc. This API creates an interface which gives a developer tools to search and interact with many of the different services provided by google. (Google Docs, Google Calender, Youtube etc...)The Google Data(GData) API has a library for many different languages; this tutorial covers the Java Client. In this tutorial I will cover installation of the GData Java client eclipse plug-in, a brief overview of many of the APIs and finally an example using the Code Search library.

If you want to install the packages yourself, you will need:

Javamail: http://www.oracle.co...jsp-139225.html
GData: http://code.google.c...t-download.html
AAnt: http://ant.apache.org/bindownload.cgi

If you would prefer instead to use eclipse and are using version 3.6. Open eclipse and go to
Help -> Install New Software -> Add
Name : whatever
Location: http://gdata-java-cl...vn/update-site/

check the box and hit next. Ensure that Google Data Feature is selected -> next -> next -> accept software license and continue through the prompts until you see the frame that has download software dependencies. Click the box that downloads them and save them where you like.After the eclipse plug-in is installed, you know have an environment in which to work with the API.

APIs contained within GData:


CodeSearch: http://code.google.c...pis/codesearch/
Developers Guide: http://code.google.c...pers_guide.html
Client Library : http://code.google.c...t-download.html

Google Code Search allows you to search public source code. The advantages from being able to search other peoples code has effectively increased my understanding of topics. It is one thing to read you open a URL and open a stream to a InputStreamReader being used by a BufferedReader, it is a completely different and much easier to understand when you see.

URL Url = new URL(“http://dreamincode.net”);
BufferedReader readIn = new BufferedReader(new InputStreamReader(Url.openStream()))

This is the benefit Google Code Search affords you. Using the API is very straight forward, it uses POSIX regexp to control everything.
Regexp: http://en.wikipedia....ular_expression
There are many different parameters to search by:
regexp Search for a regular expression
regexp	Search for a regular expression
go{2}gle  hello,\ world  ^int printk
"exact string"	Search for exact string
"compiler happy"
class:regexp      function:regexp	Search only in class and func tion names New!
class:BTree               function:laugh
class:hash.*multimap      function:.*range
file:regexp	          Search only in files or directories matching regexp
file:\.js$                XMLHttpRequest  file:include/ ioctl
package:regexp	          Search packages with names matching regexp.
(A package's name is its URL or CVS server information.)
package:perl.*\.tar\.gz   Frodo  package:linux-2.6 int\ printk
lang:regexp	          Search only for programs written in languages matching regexp
lang:lisp xml             lang:"c++" sprintf.*%s
license:regexp	          Search only for files with licenses matching regexp.
license:bsd               int\ printf   -license:gpl heapsort

To try out Code Search through your browser connect to google.com/codesearch
To understand how the API works you must understand the Google Data Protocol and how information requests are made and processed in the API.

Coding Example:

Consider that I want to query the GData so I can recieve the first link for a specific search. For example if you want to search for “Hello World.java”. The string you send to the CodeSearchService would be “lang:java%20Hello%20World”

package realircbot;

/* INSTRUCTION: This is a command line application. So please execute this template with the following arguments:

       arg[0] = search query (If query contains space, replace space by %20. For e.g. : to search for "Hello World" the argument should be Hello%20World)

* @author (Your Name Here)

import com.google.gdata.client.codesearch.CodeSearchService;
import com.google.gdata.data.codesearch.CodeSearchEntry;
import com.google.gdata.data.codesearch.CodeSearchFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import com.google.gdata.data.*;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

* This is a test template

 public class CodeSearch {
    String[] mesOut = null;
    public String codeSearch(String message){
           mesOut = message.split(" ");
           String codeSearchStatement = "";
           String returnStatement = "";
           /*The API requires that all spacing be changed to %20 */
           for(int i= 1; i<mesOut.length;i++){
               codeSearchStatement+= mesOut[i];
               if(i != mesOut.length)
               codeSearchStatement += "%20";
               System.out.println("codeSearch Inside For :" + codeSearchStatement);
           System.out.println(":" + codeSearchStatement + ":");
       try {
       // Create a new Code Search service
       CodeSearchService myService = new CodeSearchService("My Application");
       // metafeedUrl :  "http://www.google.com/codesearch/feeds/search?q="
       // Get a list of all entries
       URL metafeedUrl = new URL("http://www.google.com/codesearch/feeds/search?q="+codeSearchStatement+"");
       System.out.println("Getting Code Search entries...\n");
       CodeSearchFeed resultFeed = myService.getFeed(metafeedUrl, CodeSearchFeed.class);
       List<CodeSearchEntry> entries = resultFeed.getEntries();
       String returnResult = "";
       for(int i=0; i<entries.size(); i++) {
         CodeSearchEntry entry = entries.get(i);
         Link holder = entry.getHtmlLink();
         String theLink = holder.getHref();
         System.out.println("\t" + entry.getTitle().getPlainText());
         returnResult +=  theLink +"\n";
       returnStatement = returnResult;
       System.out.println("\nTotal Entries: "+entries.size());
     catch(AuthenticationException e) {
     catch(MalformedURLException e) {
     catch(ServiceException e) {
     catch(IOException e) {
 return returnStatement;

This class will connect to google search RSS feed at “http://www.google.com/codesearch/feeds/search?q=” and query it with codeSearchStatement.
and return the result in a CodeSearchFeed. The resultFeed is then put into a List<CodeSearchEntry> entries so that they can be processed through. Because of the way I set this up it only returns the first entry but the feed by default returns 10 at a time.

Ending Notes:

When I first started playing with Google Data I had to change my classpath to point to the collections library in google data. This can be done by right clicking on your package name going to run as: -> Run Configurations -> Classpath
Enter the directory that the GData collections library was saved to, for me it is:
C:\Users\tsama\Desktop\gdata\java\deps\ and selecting google-collect-1.0-rc1.

If you are having issues with installation check out:

Thank you for reading, let me know if you have any questions.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1