import java.io.*; import com.google.soap.search.*; public class GoogleWS { public static void main(String[] args) { String clientKey = "bIek2/NQFHIgU7a9QKKvG5KJZIb+0f01"; String directive = "search"; String directiveArg = args[0]; // Create a Google Search object, set our authorization key GoogleSearch s = new GoogleSearch(); s.setKey(clientKey); // Depending on user input, do search or cache query, then print out result try { if (directive.equalsIgnoreCase("search")) { s.setQueryString(directiveArg); GoogleSearchResult r = s.doSearch(); System.out.println("Google Search Results:"); System.out.println("======================"); System.out.println(r.toString()); } else if (directive.equalsIgnoreCase("cached")) { System.out.println("Cached page:"); System.out.println("============"); byte [] cachedBytes = s.doGetCachedPage(directiveArg); // Note - this conversion to String should be done with reference // to the encoding of the cached page, but we don't do that here. String cachedString = new String(cachedBytes); System.out.println(cachedString); } else if (directive.equalsIgnoreCase("spell")) { System.out.println("Spelling suggestion:"); String suggestion = s.doSpellingSuggestion(directiveArg); System.out.println(suggestion); } } catch (GoogleSearchFault f) { System.out.println("The call to the Google Web APIs failed:"); System.out.println(f.toString()); } } }