Google Hacks 2Nd Edition [Electronic resources]

Tara Calishain

نسخه متنی -صفحه : 209/ 177
نمايش فراداده

Hack 97. Program Google in Java

Programming the Google Web API in Java is a snap, thanks to 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, programming 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.

In addition to the googleapi.jar file included in the Google API Developer's Kit, you'll need a copy of the Java 2 Platform, Standard Edition (J2SE, http://java.sun.com/downloads/) to compile and run this hack.

9.14.1. The Code

Save the following code to a file called Googly.java:

// 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 in place of insert key here, like so:

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

9.14.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 my Googly.java source file; if you 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.

9.14.3. Running the Hack

Run Googly on the command line ["How to Run the Hacks" in the Preface], passing it your Google query, like so under Unix and Mac OS X:

% java -classpath .:googleapi.jar Googly " query words " and like so under Windows (notice the ; instead of : in the classpath):

java -classpath .;googleapi.jar Googly " query words "

9.14.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_upl ... 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++. ...