Using Google Search API from Java

Recently I’ve discovered ability to search in Google from Java program in a way different from brute “crawling”.And I’ve found out two options:

Google SOAP API

With this API you can issue search requests to Google’s index of billions of web pages and receive results as structured data, access information in the Google cache, and check the spelling of words. Google SOAP Search API is implemented as a web service and all you need is just get WSDL and call remote methods through SOAP protocol. Usage of this service is limited to 1000 requests per day.

But as you can read on SOAP API site

As of December 5, 2006, we are no longer issuing new API keys for the SOAP Search API. Developers with existing SOAP Search API keys will not be affected.

So you aren’t able to get API key (I found the old one in Internet). And Google encourages you to use the AJAX Search API, which is described below.

Example of code that perform search request and show obtained results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import com.google.soap.search.*;
import java.io.*;
public class SimpleGoogleDemo {
	public static void main(String[] args) {
		// Create a Google Search object, set our authorization key
		GoogleSearch s = new GoogleSearch();
		String clientKey="xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
		s.setKey(clientKey);
		s.setQueryString("vera.org.ua");
		GoogleSearchResult result = null;
		// Depending on user input, do search or cache query, then print out result
		try {
			result = s.doSearch();
		}
		catch (GoogleSearchFault f) {
			System.out.println("The call to the Google Web APIs failed:");
			System.out.println(f.toString());
		}    // if we made it here, the search went through
		System.out.println("Google Search Results:");
		System.out.println("======================");
		if (result!= null)
			System.out.println(result.toString());
	}
}

To run this code you need to have googleapi.jar in your classpath. This jar file contains very useful and easy-to-use class com.google.soap.search.GoogleSearch, which does all search work.

Google AJAX API

Usage of this unlimited and you can make as many request as you want. Despite the fact that Google in this API FAQ say “The Google AJAX Search API is currently available only for websites.” ability to use it exists :-)

Example of code that perform search request and show obtained results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.io.*;
import java.net.*;
 
public class GoogleAJAXSearchAPI {    
 
	private static String endpointURL = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=vera";
 
	public static void main(String[] args) throws Exception {
		URLConnection uc = new URL(endpointURL).openConnection();
		HttpURLConnection connection = (HttpURLConnection) uc;
		connection.setDoOutput(true);
		connection.setRequestMethod("GET");
		connection.connect();
		String line;
		InputStream inputStream = null;
		try {
			inputStream = connection.getInputStream();
		} catch (IOException e) {
			inputStream = connection.getErrorStream();
		}
		BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
		while ((line = rd.readLine()) != null) {
			System.out.println(line);
		}
	}
}

To run this code you need to can get API key here.

So, have fun :)

PS Look at this too - Java OO wrapper for Google AJAX API


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

6 Responses to “Using Google Search API from Java”

  1. I’ve recently looked for API to some giant search engine.

    And stopped on Yahoo’s API. Reason is simple - they’re provide SOAP and REST access to their engine. More handy approach, eh?

  2. But Google is Google :-)

  3. helloo.. could anybody help me..
    here in the above general code of google soap protocol.. i need google api jar file
    in the above link i am not able to download, there was an internal error with webserver..

    so could anybody forward to my mail account pleasee..

    harsha.siri@gmail.com

    thanking you forever…. harsha

  4. >>in the above link i am not able to download, there was an internal error with webserver..

    Sorry for inconvenience. This problem was fixed.

  5. Vira,

    Can you explain the endpointURL string? I am trying to use my own google api key in the string but I am getting this back:

    {”responseData”: null, “responseDetails”: “invalid version”, “responseStatus”: 400}

    thanks.

  6. kulpster,

    Checked this example again and it doesn’t works anymore :( Try to use this url “http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=query_str” . It’s much simpler and works for me. It returns 4 result at a time, to get next 4 use “start” parameter.

    BTW, according to google docs api key is optional for now. If you provide key and some error occurs while searching Google can help work it out :)

Leave a Reply