Solved

Java program to display search results using google api

Posted on 2010-11-11
1
869 Views
Last Modified: 2012-05-10
The task is to send a search string to google api and it should display the list of matching search results.
The code mentioned is not working as the google API is deprecated.Please suggest a solution.
//Copyright (C) 2002, Google, Inc. 

import java.io.*;

import com.google.soap.search.GoogleSearch;

import com.google.soap.search.GoogleSearchFault;

import com.google.soap.search.GoogleSearchResult;



/**

 * 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:<br>

   **   <tt>java com.google.soap.search.GoogleAPIDemo [key] search Foo</tt><br>

   **   <tt>java com.google.soap.search.GoogleAPIDemo [key] cached http://www.google.com/</tt>

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

   **/

  public static void main(String[] args) {

    // Parse the command line

    /*if (args.length != 3) {

      printUsageAndExit();

    }*/

    String clientKey ="ABQIAAAAAWMbIopLMDa2odzrkqvrYxQFvBf5fu0PMVBF0-M462aG3BtQpRTfVI3jxVQqDynQuOLaaovN-sPBng";// args[0];

    String directive = "search"; //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);

    

    //reading input data from a file

    try {

    FileInputStream fstream = new FileInputStream("list.txt");

    // Get the object of DataInputStream

    DataInputStream in = new DataInputStream(fstream);

        BufferedReader br = new BufferedReader(new InputStreamReader(in));

    String strLine;

    //Read File Line By Line

    while ((strLine = br.readLine()) != null)  

    {

      // Print the content on the console

      System.out.println (strLine);

      // Depending on user input, do search or cache query, then print out result

        if (directive.equalsIgnoreCase("search"))

        {

        s.setQueryString(directiveArg);

         s.setQueryString(strLine);

         String temp =strLine;

         s.setSoapServiceURL("http://maps.google.com");

    	 //s.setProxyHost("");

         //s.setProxyHost("");

         //s.setProxyPort(8080);

    	 s.setSafeSearch(true);

    	 //s.setProxyUserName("");

    	 //s.setProxyPassword("");

    	  s.setMaxResults(5);

    	  System.out.println("Starting search engine....");

    	  

    	  //s.setLanguageRestricts("English");

    	 s.setSoapServiceURL("http://maps.google.com");

       	 System.out.println(s.doSpellingSuggestion(strLine));

       	 System.out.println("B4 search results");

    	  GoogleSearchResult  r = s.doSearch();

    	  System.out.println("after search results");

        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(strLine);

        // 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(strLine);

        System.out.println(suggestion);

      } 

      else {

        printUsageAndExit();

           }

    }

    //Close the input stream

      in.close();

    } 

      catch (GoogleSearchFault f) {

      System.out.println("The call to the Google Web APIs failed:");

      System.out.println(f.toString());

    }

      catch (Exception e){//Catch exception if any

          System.err.println("Error: " + e.getMessage());

        }

  }



  private static void printUsageAndExit() {

    System.err.println("Usage: java " + 

                       GoogleAPIDemo.class.getName() +

                       " <client-key>" +

                       " (search <query> | cached <url> | spell <phrase>)");

    System.exit(1);

  }

}

Open in new window

0
Comment
Question by:ezsas
1 Comment
 
LVL 3

Accepted Solution

by:
alpharom earned 500 total points
ID: 34110654
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now