oogle has introduced a Web API service that enables developers to program search engine functionality into their applications. With the Google Web APIs service, a program can query more than 2 billion Web documents quickly and easily. Applications with this functionality allow users to schedule regular search requests that can help monitor the Web for new information on a subject or offer comparative analyses of the amount of information available on different subjects over time.
The Google Web APIs service provides a SOAP (Simple Object Access Protocol) interface to search Google's index, accessing information and Web pages from its cache and checking the spelling of words against Google's standard search syntax. With its use of the SOAP and WSDL standards, Google allows developers to program in three environments: Java, Perl, or Visual Studio .NET. In this article, I use a sample program I coded (GoogleSearchDemo.java) to demonstrate how to use the Google Web APIs service with Java code.
First you need to download the Google kit from http://www.google.com/apis/download.html. The free downloadable kit contains:
- A complete API reference describing the semantics of method calls and fields
- Sample SOAP request and response messages
- A Google Web API WSDL file
- A Java library, example program, and Javadoc documentation
- A sample .NET program
Create a Google Web APIs service account. Use your account username and password to log in and get an account key. Note that Google limits each developer who registers for the Web APIs service to 1,000 queries per day.
Now you're ready to dive into the code. The following classes are included in the
import com.google.soap.search.GoogleSearch; The
GoogleSearch class provides access to the Google Web APIs, as well as Google search functions and cached pages via SOAP.
GoogleSearchResult encapsulates presents the complete results from each Google Web APIs search call. You should call the get methods only on this object; the fields are filled in when a search result is returned.
GoogleSearchResultElement contains an individual search result component of a
GoogleSearchFault is an exception that encapsulates various errors that can result from a Google API call.