www.pudn.com > google_examples.zip > MyGoogle.java, change:2002-05-05,size:1691b


import GoogleSearch.*; 
import java.net.URL; 
 
/* 
 
 MyGoogle.java 
 
 This is a simple client application that invokes the GoogleSearch 
 web service without using the client classes in googleapi.jar.  
 
 Sample output: 
 
 E:\test>java MyGoogle "Koko Taylor" 
 Total no. of items found: 21900 
 http://www.rollingstone.com/artists/default.asp?oid=2195 
 http://www.angelfire.com/az/jnesbit/koko.html 
 http://www.blues.org/laa/1999/kokotaylor.html 
 http://centerstage.net/music/whoswho/KokoTaylor.html 
 http://www.delafont.com/music_acts/koko-taylor.htm 
 
*/ 
 
public class MyGoogle { 
 
	public static void main(String[] args) { 
	 
		if (args.length != 1) { 
			System.out.println("Usage: java MyGoogle <query>"); 
			System.exit(0); 
		} 
 
		String key = "insert_your_client_key_here";  
		String q = args[0]; 
		int start = 1; 
		int maxResults = 5; 
		boolean filter = true; 
		String restrict = null; 
		boolean safeSearch = true; 
 
		try { 
			GoogleSearchServiceLocator locator = new GoogleSearchServiceLocator(); 
			URL serviceURL = new URL(locator.getGoogleSearchPortAddress());  
			GoogleSearchBindingStub client = new GoogleSearchBindingStub(serviceURL, null); 
			GoogleSearchResult result = client.doGoogleSearch(key, q, start, maxResults, filter, restrict, safeSearch, null, null, null); 
			System.out.println("Total no. of items found: " + result.getEstimatedTotalResultsCount()); 
			ResultElement[] elements = result.getResultElements(); 
			for (int i = 0; i < maxResults; i++) { 
				ResultElement element = elements[i]; 
				System.out.println(element.getURL()); 
			} 
 
		} catch (Exception e) { 
			e.printStackTrace(); 
			System.exit(0); 
		} 
	} 
 
}