Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

"Googlize" Your Java Apps to Search Billions of Web Pages : Page 2

With the Google Web APIs service, a program can query more than 2 billion Web documents quickly and easily. Vikas Pandya uses a sample program to demonstrate how you can use the Google Web APIs service to program search engine functionality into your Java applications.


advertisement
Download my sample Google Web API program, GoogleSearchDemo.java. Create an instance of the GoogleSearch class and set the key that Google has provided. Keep in mind that Google won't let you use its search functionality until you set the key:

GoogleSearch search = new GoogleSearch(); search.setKey("yourkey");

After setting the key, set the query string for search:



search.setQueryString("cross language barriers for SOAP");

Now you need to invoke the Google search and store the return results:

GoogleSearchResult result = search.doSearch();

Next, iterate through the results:

GoogleSearchResultElement[] re = result.getResultElements(); for ( int i1 = 0; i1 < re.length; i1++ ) { System.out.println("<a href=\"" + re[i1].getURL() + "\">" + re[i1].getTitle() + "</a><br />"); }

Before compiling the code, you need to put the googleapi.jar file into your classpath.

What If I'm Behind a Firewall?
If you are running behind a firewall, Google search will return the following SOAP exception when you try to execute it:

com.google.soap.search.GoogleSearchFault:
[SOAPException: faultCode=SOAP-ENV:Client;
msg=Error opening socket: api.google.com;
targetException=java.lang.IllegalArgumentException:
Error opening socket: api.google.com]

To get your code to work behind a firewall proxy, you'll need to modify the GoogleSearch class and implement the following four methods of the org.apache.soap.transport.http.SOAPHTTPConnection class:

  • public void setProxyHost(String s){}
  • public void setProxyPort(int i){}
  • public void setProxyUserName(String s){}
  • public void setProxyPassword(String s){}

If you don't want to modify your existing class, download patgoogle.jar, which is a patch for firewall proxies. It contains the GoogleSearch.class with this added modification. Be sure to place patgoogle.jar before googleapi.jar in the classpath, since a modified GoogleSearch class exists in patgoogle.jar. Hopefully, Google will include these changes in future releases of its kit so developers don't have to add any patches. (In the sample program, I incorporated calls for firewall proxies too. If you are not running behind a firewall just comment those calls out.)

As Easy As It Seems
Using the Google Web APIs service as I've demonstrated, your application can search billions of Web page—and you don't need to use any complicated code.




Vikas Pandya is a Sun-certified Java software engineer. He currently works as an XML/XSL, Java developer for Bank of America in Charlotte, NC. He also consults for Exedem Technology, focusing on XML Web Services and related technologies. Contact him at vikasdp@yahoo.com or vikas.pandya@exedem.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date