Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1940431
  • 博文数量: 606
  • 博客积分: 9991
  • 博客等级: 中将
  • 技术积分: 5725
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-17 19:07
文章分类

全部博文(606)

文章存档

2011年(10)

2010年(67)

2009年(155)

2008年(386)

分类:

2009-03-03 16:51:20

文件: googleaipdemo.rar
大小: 20KB
下载: 下载

/* Copyright (C) 2002, Google, Inc. */
package com.google.soap.search;
import java.io.*;
/**
 * The GoogleAPIDemo is a demonstration of using the Google Web APIs for
 * search and fetching pages from the cache.
 *
 * @see com.google.soap.search.GoogleSearch
 * @see com.google.soap.search.GoogleSearchResult
 */

public class GoogleAPIDemo {
  /** Demonstration program to call the Google Web APIs service.
   ** Usage:

   ** java com.google.soap.search.GoogleAPIDemo [key] search Foo

   ** java com.google.soap.search.GoogleAPIDemo [key] cached
   ** java com.google.soap.search.GoogleAPIDemo [key] spell "britnay spars"
   **/

  public static void main(String[] args) {
    // Parse the command line

    if (args.length != 3) {
      printUsageAndExit();
    }
    String clientKey = args[0];
    String directive = args[1];
    String directiveArg = args[2];
    // Report the arguments received

    System.out.println("Parameters:");
    System.out.println("Client key = " + clientKey);
    System.out.println("Directive = " + directive);
    System.out.println("Args = " + directiveArg);
    // 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);
      } 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);
  }
}

阅读(1364) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~