CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2010
    Posts
    10

    Smile Get total search results from Google

    Hi,

    Just wondering if anyone know whether if there is a way that I can directly send search query
    to Google web service from within a java application and get the result back?

    Basically I just need to know the total number of pages the result return. I know there used to be
    a Google API called SOAP that let you do that but they discontinued it -_- and replace it with AJAX Search, which I am not sure how can I get it to work within java.

    If anyone know a way or have other alternatives please let me know.

    Cheer!

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Get total search results from Google

    You can use the Google "AJAX" calls just like a regular HTTP call in Java. From the Google AJAX API:

    the API exposes a raw RESTful interface that returns JSON encoded results that are easily processed by most languages and runtimes
    So, you will get the search results back in JSON format which can easily be parsed.

    So you can do something like this:

    Code:
    String query = "foo bar";
    URL url = new  URL("http://ajax.googleapis.com/ajax/services/search/web?start=0&rsz=large&v=1.0&q="  + query);
    URLConnection connection = url.openConnection();
    connection.addRequestProperty("Referer", "http://www.somewebsite.com"); //this isn't needed
    
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    This will make the call to the Google AJAX site and return the response in the form of JSON. Now you just need to figure out how to parse it :P

    There is a JSON Library that you can download and use in your application at http://www.json.org/java/.
    Last edited by ProgramThis; June 2nd, 2010 at 08:11 AM.

  3. #3
    Join Date
    Apr 2010
    Posts
    10

    Thumbs up Re: Get total search results from Google

    Oh sweet, thank you very much!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured