import java.io.IOException;
import java.util.ArrayList;

import com.google.soap.search.GoogleSearch;
import com.google.soap.search.GoogleSearchFault;
import com.google.soap.search.GoogleSearchResult;
import com.google.soap.search.GoogleSearchResultElement;

/**
 * GoogleSearch.
 * 
 * @author Jose Sandoval
 */
public class MyGoogleSearch {
	private static String CLIENT_KEY = "YOU_HAVE_TO_GET_YOUR_OWN_KEY_FROM_GOOGLE";

	/**
	 * Search Google.
	 * 
	 * @param queryString String
	 * @return ArrayList Contains SimpleSearchResult
	 * @throws IOException
	 */
	public static ArrayList doSearch(String queryString) throws IOException {
		ArrayList searchResults = new ArrayList();
		GoogleSearch search = new GoogleSearch();
		search.setKey(CLIENT_KEY);
		
		try {
			search.setQueryString(queryString.trim());
			GoogleSearchResult results = search.doSearch();
			GoogleSearchResultElement[] resultElements = results.getResultElements();
			
			for (int i = 0; i<resultElements.length; i++) {
				searchResults.add(new SearchResultBean(resultElements[i].getTitle(), resultElements[i].getURL(), resultElements[i].getSnippet()));
			}
		} catch (GoogleSearchFault f) {
			throw new IOException("The call to the Google Web APIs failed: " + f.getMessage());
		} catch (Exception e) {
			throw new IOException("Thread Problem.");
		}
		
		return searchResults;
	}
}
Syntax Highlighting created using the com.Ostermiller.Syntax package.
Sunday, March 20 2005 at 00:43