package com.google.soap.search; import java.io.*; import java.awt.*; import java.awt.event.*; public class GoogleAPIDemo implements ActionListener { static TextField TextFieldInput; static Button BtnSearch; static Button BtnNext; static Button BtnGetCachedPage; static Button BtnSpell; static TextArea TextAreaOutput; int resultindex=1; public GoogleAPIDemo() { TextFieldInput=new TextField(50); BtnSearch=new Button("search"); BtnNext=new Button("Next"); BtnGetCachedPage=new Button("GetCachedPage"); BtnSpell=new Button("Spell"); TextAreaOutput=new TextArea(); BtnSearch.addActionListener(this); BtnNext.addActionListener(this); BtnGetCachedPage.addActionListener(this); BtnSpell.addActionListener(this); } public static void main(String[] args) { Frame f=new Frame("GoogleAPIDemo"); GoogleAPIDemo g=new GoogleAPIDemo(); Panel up=new Panel(); up.add(TextFieldInput); up.add(BtnSearch); up.add(BtnNext); up.add(BtnGetCachedPage); up.add(BtnSpell); f.add(up,BorderLayout.PAGE_START); f.add(TextAreaOutput,BorderLayout.CENTER); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); f.setSize(800,600); f.setVisible(true); } public void actionPerformed(ActionEvent e) { if ( e.getSource()==BtnSearch ) { String clientKey="F4ZdLRNQFHKUvggiU+9+60sA8vc3fohb"; String directive="search"; String directiveArg=TextFieldInput.getText(); GoogleSearch s=new GoogleSearch(); s.setKey(clientKey); s.setStartResult(0); resultindex=0; try { s.setQueryString(directiveArg); GoogleSearchResult r=s.doSearch(); TextAreaOutput.setText(r.toString()); } catch (GoogleSearchFault f) { System.out.println("The call to the Google Web APIs failed:"); System.out.println(f.toString()); } resultindex+=10; } if ( e.getSource()==BtnNext ) { String clientKey="F4ZdLRNQFHKUvggiU+9+60sA8vc3fohb"; String directive="search"; String directiveArg=TextFieldInput.getText(); GoogleSearch s=new GoogleSearch(); s.setKey(clientKey); s.setStartResult(resultindex); try { s.setQueryString(directiveArg); GoogleSearchResult r=s.doSearch(); TextAreaOutput.setText(r.toString()); } catch (GoogleSearchFault f) { System.out.println("The call to the Google Web APIs failed:"); System.out.println(f.toString()); } resultindex+=10; } if ( e.getSource()==BtnGetCachedPage ) { String clientKey="F4ZdLRNQFHKUvggiU+9+60sA8vc3fohb"; String directive="cached"; String directiveArg=TextFieldInput.getText(); GoogleSearch s=new GoogleSearch(); s.setKey(clientKey); try { byte [] cachedBytes = s.doGetCachedPage(directiveArg); String cachedString = new String(cachedBytes); TextAreaOutput.setText(cachedString); } catch (GoogleSearchFault f) { System.out.println("The call to the Google Web APIs failed:"); System.out.println(f.toString()); } } if ( e.getSource()==BtnSpell ) { String clientKey="F4ZdLRNQFHKUvggiU+9+60sA8vc3fohb"; String directive="spell"; String directiveArg=TextFieldInput.getText(); GoogleSearch s=new GoogleSearch(); s.setKey(clientKey); try { String suggestion = s.doSpellingSuggestion(directiveArg); TextAreaOutput.setText(suggestion); } catch (GoogleSearchFault f) { System.out.println("The call to the Google Web APIs failed:"); System.out.println(f.toString()); } } } /* if (args.length != 3) { printUsageAndExit(); } String clientKey = args[0]; String directive = args[1]; String directiveArg = args[2]; System.out.println("Parameters:"); System.out.println("Client key = " + clientKey); System.out.println("Directive = " + directive); System.out.println("Args = " + directiveArg); GoogleSearch s = new GoogleSearch(); s.setKey(clientKey); 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); 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); } else { printUsageAndExit(); } } catch (GoogleSearchFault f) { System.out.println("The call to the Google Web APIs failed:"); System.out.println(f.toString()); } } private static void printUsageAndExit() { System.err.println("Usage: java " + GoogleAPIDemo.class.getName() + " " + " (search | cached | spell )"); System.exit(1); } */ }