
Using the Google Web API in Java, is very simple to get started. First and foremost, all searches require a license key, which is free. This key enables up to 1000 searches per day. To get a key, visit
Get the Google API Key
http://www.google.com/apis/
// MyGoogle.java
import com.google.soap.search.*;
import java.io.*;
public class MyGoogle
{
// Your Google API developer's key.
private static String googleKey = "
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] MyGoogle
System.exit(1);
}
// Create a new GoogleSearch object
GoogleSearch s = new GoogleSearch( );
try {
s.setKey(googleKey);
s.setQueryString(args[0]); // Google query from the commandline
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");
}
// Any Errors
} catch (GoogleSearchFault f) {
System.out.println("GoogleSearchFault: " + f.toString( ));
}
}
}