package GoogleSearch; /* GoogleClient.java by Jeffrey K. Brown Lots of stuff taken from demo at http://www.bawsug.org/archives/000008.html since I was reading their stuff before I decided to make my GoogleClient do my searches. WSDL2Java makes the setup of the connection to the web service very simple. Remember what the connection to the TemperatureConversion.jws Web Service involved? Compare that to the WSDL2Java way... */ import java.io.*; public class GoogleClient { // This is my Google Developer's Key. You'll have to get your own // Key created if you want to use Google's Web Service. public static final String key = "l;kj@LkjfaZLKJlkja;;;ke332mLKSGJ"; // This method passes a word, and Google reads it and checks to see if we might // have spelled the word wrong, and then suggests a new word, just in case. public void spellsuggest(String phrase) { try { // WSDL2Java cuts out the linking and setting up and declaring // of functions, return types and parameter types and replaces // it with this quick and easy way to link to the Web Services. GoogleSearchService service = new GoogleSearchServiceLocator(); GoogleSearchPort google = service.getGoogleSearchPort(); String result = google.doSpellingSuggestion(key, phrase); if (result.equals(null)) { System.out.println("No Suggestions for: "+phrase); } else { System.out.println("Suggestions for: "+phrase); System.out.println("Did You Mean: "+result); } } catch (NullPointerException npe) { System.out.println("No Suggestions for "+phrase); } catch (Exception e) { System.out.println("Well, it ain't working...Here's why:"); e.printStackTrace(); } } // This method performs a Google search and then returns the results. public void searchString(String query) { try { // Again, notice the quick and easy setup. GoogleSearchService service = new GoogleSearchServiceLocator(); GoogleSearchPort google = service.getGoogleSearchPort(); // These are the parameters and types to the doGoogleSearch method. int start=0; int max=10; boolean filter=true; String restrict=""; boolean safe=true; String lang="lang_en"; String encoding="UTF-8"; // The doGoogleSearch method returns a complex type, so instead of letting // the user figure out how to handle it, the WSDL2Java creates the types to // handle the result and parse them neatly. GoogleSearchResult gsr = google.doGoogleSearch(key,query,start,max,filter,restrict,safe,lang,encoding,encoding); ResultElement[] rs = gsr.getResultElements(); // Finally, we just print the results. for (int i=0; i" + rs[i].getTitle() +""; } else { liststring = liststring + "
  • " + rs[i].getTitle() +"
  • "; } //System.out.println(""+i+". "+rs[i].getTitle()+"("+rs[i].getURL()+")"); } liststring = liststring + ""; } catch (Exception e) { System.out.println("Exception during Search:"); e.printStackTrace(); } return liststring; } public static void main (String[] args) { GoogleClient g = new GoogleClient(); // Here, I was experimenting with the // Spelling Suggestion Capability // ------------------------------ // g.spellsuggest("lllama"); // g.spellsuggest("ggoat"); // g.spellsuggest("goat"); //g.searchString("jkb"); //g.searchString("jkb world"); //System.out.println(g.getJkbWorldSearch("jkb world")); try { System.out.println("Building HTML..."); PrintWriter out = new PrintWriter (new FileWriter("google.html")); out.println(""); out.println(""); out.println("JkbWorld Results from Google Web Service"); out.println(""); out.println(""); out.println(""); out.println("

    JkbWorld Results from Google Web Service

    "); out.println("
    "); out.println("

    Google Ranking for "jkb":
    "); out.println(g.getJkbWorldSearch("jkb")); out.println("

    "); out.println("
    "); out.println("

    Google Ranking for "jkb world":
    "); out.println(g.getJkbWorldSearch("jkb world")); out.println("

    "); out.println(""); out.println(""); out.close(); System.out.println("Complete!"); } catch (Exception e) { e.printStackTrace(); } } }