Using Google Web Services API in Java

Java Discuss, Using Google Web Services API in Java at Programmers Lounge forum; Google uses SOAP and WSDL standards to allow access from most programming languages. In addition to the SOAP access, Google ...


Go Back   Gamerz Needs - For All Your Gaming Needs! > Technology Zone > Programmers Lounge > Java
Forgot Password? | Sign Up!

Notices

Advertisement
   

Closed Thread
 
Bookmark this Thread Tools Display Modes
  #1  
Old 08-23-2006, 09:43 AM
M0NK3Y's Avatar
Armenia
 
Last Online: Today 01:41 PM
Join Date: Mar 2006
Location: Boss World :D
Posts: 2,671
Thanks: 225
Thanked 540 Times in 348 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 14
M0NK3Y will become famous soon enough
Points: 43,665.01
Bank: 499.61
Total Points: 44,164.62
Banana *yey* i <3 banana milkshake - Demonite cuz you back again lol - GameLordquest wanne do it again? xD - DeathniteR AYE AYE PIRATE! DRINK YOUR BEERZ! - M0NK3Y Im a M0NK3Y Pirate so.. i need a banana! :D - M0NK3Y 
No Message - M0NK3Y No Message - M0NK3Y No Message - M0NK3Y No Message - M0NK3Y 
Black - Andrex2x Burgundy - Demonite Black - Demonite Burgundy - Demonite 
Send a message via ICQ to M0NK3Y Send a message via AIM to M0NK3Y Send a message via MSN to M0NK3Y Send a message via Yahoo to M0NK3Y
Exclamation Using Google Web Services API in Java

Google uses SOAP and WSDL standards to allow access from most programming languages. In addition to the SOAP access, Google has created a set of Java wrapper classes that encapsulates access to the Google Web APIs. This tutorial discusses the Java classes and the functionality of the Google web services.The Google web services allow you to query Google's web index and to access the Google spell check engine. When performing a search you have access to most features available when searching on Google's web site:
  • Retrieve a cached web page from Google
  • Enable or disable SafeSearch
  • Limit the search results by language
Currently, there are limitations on Google's Web API. Some features that aren't currently available through Google's Web API:
  • Search is limited to the Web search. Currently, there isn't any access to search in the Images, Groups, News, Froogle, or Local categories.
  • The search modifies are limited to a subset of the search modifiers that are allowed on google.com (See Related Links). For example, the "site:" modifier is allowed from the web api only if additional search terms are included. Google.com allows for "site:" modifier to be used without any additional search terms to list all pages in a site that have been indexed. In addition, some additional search modifies such as "movie:" to display movie times specified by a zip code.
  • Searching in Google's Print index is not currently available.
  • Google's calculator, currency conversion, word definition, stock quotes, or weather are not currently available through the Google Web API.
To start using the Google Web API, you will need to download the Google Web APIs developer's kit and register for a Google license key on Google's developer page (see Related Links). Once you have downloaded and unzipped the Google developer's kit, include the googleapi.jar file in your Java class path.
You will need to instantiate an instance of the GoogleSearch class and set the key that was provided by Google.
GoogleSearch googleSearch = new GoogleSearch();

//Set the key provided by Google
googleSearch.setKey("your key");

Next, we need to set the query string that you want to search for in the Google index. If you want to use any search modifiers, include them in the query string. To limit our search results, we set the maximum number of matches to 10.
//Search query string
googleSearch.setQueryString("population of Japan");

//Limit the search results to 10
googleSearch.setMaxResults(10);

For our final steps, we perform the search. After the search is complete, we get the search result elements. The search result elements contain the page title, url, and page snippet.
GoogleSearchResult searchResult = googleSearch.doSearch();
GoogleSearchResultElement[] results = searchResult.getResultElements();
On a final note, the search result elements contain HTML formatting to show the hits within the results. In our example, we use a Java's regular expression support to remove the HTML tags. This is a quick and easy way to remove the HTML tags from text.
String title = results[index].getTitle();
title = title.replaceAll("<.*?>", "");
The complete Google Web API example is below.
package tutorial;

import com.google.soap.search.*;

public class Google
{
public Google()
{
try
{
GoogleSearch googleSearch = new
GoogleSearch();

//Set the clientKey to key provided by Google
googleSearch.setKey("your key");

//Search query string
googleSearch.setQueryString("population of Japan");

//Limit the search results to 10
googleSearch.setMaxResults(10);

GoogleSearchResult searchResult = googleSearch.doSearch();
GoogleSearchResultElement[] results = searchResult.getResultElements();

if (results.length == 0)
{
System.out.println("No match found.");
}
else
{
for (int index = 0; index < results.length; index++)
{
//Title
String title = results[index].getTitle();
title = title.replaceAll("<.*?>", "");
System.out.println(title);

//URL
String url = results[index].getURL();
System.out.println(url);

//Title
String summary = results[index].getSnippet();
summary = summary.replaceAll("<.*?>", "");
System.out.println(summary);

System.out.println();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
Google googleDemo = new Google();
}
}

Thank me if you liked it! xD

Credits go to Kevin Long.
__________________
The Following User Says Thank You to M0NK3Y For This Useful Post:
dragoon (08-23-2006)
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Advertisement
   


Main Navigation
Home
GzN Forums
GzN Games
GzN News
Top Games
GzN Cheats
GzN Articles
GzN Reviews
GzN Downloads
User Control Panel
Advertising
RSS Feed
2Moons
Adventure Quest
AirRivals
America's Army
Anarchy Online
Archlord
Audition
Battlefield Series
Cabal Online
Call Of Duty Series
Combat Arms
Conquer Online
Counter Strike
Day of Defeat
Deicide Online
Diablo Series
Doom Series
Drift City
Enemy Territory
Eudemons Online
Final Fantasy
Flyff (Fly For Fun)
General Game Discussion
Ghost Online
Granado Espada
Grand Theft Auto Series
Guild Wars
Gunbound
Gunz Online
Habbo Hotel
Half-Life 2
Hero Online
KartRider
Knights Online
Maple Story
Medal of Honor
MU Online
Neopets
Pangya
Quake Series
Ragnarok Online
Rappelz
Rakion
Red Orchestra
Rose Online
Runescape
Scions of Fate
Silkroad Online
Sims Series
Soldier Front
Starcraft
Tales of Pirates
Tibia
The Ship
Trickster Online
TS Online
Unreal Tournament
War Rock
WolfTeam
World of Warcraft & Series
Affiliates
COD4 Hacks
BF2 Hacks


All times are GMT -8. The time now is 03:19 PM.