Google Hacks Free Open Book

Google Hacks

Previous Section Next Section

Hack 56 Programming the Google Web API with Java

figs/moderate.giffigs/hack56.gif

Programming the Google Web API in Java is a snap, thanks to all the functionality packed into the Google Web API Developer's Kit.

Thanks to the Java Archive (JAR) file included in the Google Web API Developer's Kit [Section 5.4], programming to the Google API in Java couldn't be simpler. The googleapi.jar archive includes com.google.soap.search, a nice clean wrapper around the underlying Google SOAP, along with the Apache Software Foundation's open source Crimson (http://xml.apache.org/crimson) XML parser and Apache SOAP (http://xml.apache.org/soap/) stack, among others.

You'll need a copy of the Java 2 Platform, Standard Edition (J2SE, http://java.sun.com/downloads/) to compile and run this hack.

56.1 The Code

// Googly.java
// Bring in the Google SOAP wrapper
import com.google.soap.search.*;
import java.io.*;
public class Googly {
  // Your Google API developer's key
  private static String googleKey = "insert key here"; 
  public static void main(String[] args) {
    // Make sure there's a Google query on the command-line
    if (args.length != 1) {
      System.err.println("Usage: java [-classpath classpath] Googly <query>");
      System.exit(1);
    }
    // Create a new GoogleSearch object
    GoogleSearch s = new GoogleSearch(  );
    try {
    
      s.setKey(googleKey);
      s.setQueryString(args[0]); // Google query from the command-line
      s.setMaxResults(10);
      // Query Google
      GoogleSearchResult r = s.doSearch(  );
      // Gather the results
      GoogleSearchResultElement[] re = r.getResultElements(  );
      
      // Output
      for ( int i = 0; i < re.length; i++ ) {
     System.out.println(re[i].getTitle(  ));
     System.out.println(re[i].getURL(  ));
     System.out.println(re[i].getSnippet(  ) + "\n");
      }
    
    // Anything go wrong?
    } catch (GoogleSearchFault f) {
      System.out.println("GoogleSearchFault: " + f.toString(  ));
    }
  }
  
}

Be sure to drop in your own Google developer's key Section 5.5 (e.g., 12BuCK13mY5h0E/34KN0cK@ttH3Do0R) in place of "insert key here":

// Your Google API developer's key
private static String googleKey = "12BuCK13mY5h0E/34KN0cK@ttH3Do0R";

56.2 Compiling the Code

To successfully compile the Googly application, you'll need that googleapi.jar archive. I chose to keep it in the same directory as as my Googly.java source file; if you've put it elsewhere, adjust the path after -classpath accordingly.

% javac -classpath googleapi.jar Googly.java

This should leave you with a brand new Googly.class file, ready to run.

56.3 Running the Hack

Run Googly on the command line, passing it your Google query, like so:

% java -classpath .:googleapi.jar Googly "query words"

56.4 The Results

% java -classpath .:googleapi.jar Googly "Learning Java"
oreilly.com -- Online Catalog: Learning Java  
http://www.oreilly.com/catalog/learnjava/
For programmers either just migrating to Java or already working 
steadily in the forefront of Java development, Learning Java gives 
a clear, systematic   ...    
oreilly.com -- Online Catalog:   Learning     Java  , 2nd Edition
http://www.oreilly.com/catalog/learnjava2/
This new edition of Learning Java has been expanded and updated for
Java 2 Standard Edition SDK 1.4. It comprehensively addresses ...    
... 
Java Programming...From the Grounds Up / Web Developer
http://www.webdeveloper.com/java/java_programming_grounds_up.html
... WebDeveloper.com. Java Programming... From the Grounds Up. by 
Mark C. Reynolds ... Java Classes and Methods. Java utilizes the 
basic object technology found in C++. ...    
    Previous Section Next Section


         Main Menu
    Main Page
    Table of content
    Copyright
    Dedication
    Credits
    Foreword
    Preface
    Chapter 1. Searching Google
    Chapter 2. Google Special Services and Collections
    Chapter 3. Third-Party Google Services
    Chapter 4. Non-API Google Applications
    Chapter 5. Introducing the Google Web API
    5.1 Hacks #50-59
    5.2 Why an API?
    5.3 Signing Up and Google's Terms
    5.4 The Google Web APIs Developer's Kit
    5.5 Using the Key in a Hack
    5.6 What's WSDL?
    5.7 Understanding the Google API Query
    5.8 Understanding the Google API Response
    Hack 50 Programming the Google Web API with Perl
    Hack 51 Looping Around the 10-Result Limit
    Hack 52 The SOAP::Lite Perl Module
    Hack 53 Plain Old XML, a SOAP::Lite Alternative
    Hack 54 NoXML, Another SOAP::Lite Alternative
    Hack 55 Programming the Google Web API with PHP
    Hack 56 Programming the Google Web API with Java
    Hack 57 Programming the Google Web API with Python
    Hack 58 Programming the Google Web API with C# and .NET
    Hack 59 Programming the Google Web API with VB.NET
    Chapter 6. Google Web API Applications
    Chapter 7. Google Pranks and Games
    Chapter 8. The Webmaster Side of Google
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele