JAVA Implementation of calling Google APIs (Web Service)

Source: Internet
Author: User
Tags email account

Lab content

Learn how to call existing Web Services.

Write programs and call

No more nonsense about Web Services. Let's talk about how to use it.

1. You need the Java environment to http:. // java.sun.com/j2se. You can download Java Development Kit

2. The Google api jar file is required. You can download it at http://www.google.com/apis. Add classpath.

3. Apply for a Google access account. Currently, Google supports free application. You need to use an email account to obtain a new account. Note: currently, the free account supports a maximum of 1000 queries per day. Application address: http://www.google.com/apis.

Enable your account in your mailbox after applying, and then send another email to your mailbox. Obtain your license key.

The format of the SOAP request is not mentioned. Google directly provides the API. Do not waste googleapi. jar.

4. The following is an example

Package daxigua;

Import com. Google. Soap. Search .*;
Import java. Io .*;

Public class googleapitest {
Public static void main (string [] ARGs ){

String clientkey = "clientkey"; // The obtained license key, not the account name.
String query = "csdn ";
Googlesearch S = new googlesearch ();
S. setkey (clientkey );
Try {
S. setquerystring (query );
S. setlanguagerestricts ("lang_cn ");
Googlesearchresult r = S. dosearch ();
System. Out. println ("Google search results:" + R. tostring ());
Filewriter fw = new filewriter ("result.txt ");
FW. Write (R. tostring ());
FW. Close ();
} Catch (googlesearchfault f ){
System. Out. println ("the call to the Google Web APIs failed:" +
F. tostring ());
} Catch (exception e ){
E. printstacktrace ();
}
}
}

For specific use, refer to the doc api.zip doc directory. There are 6 classes in total.

The Web Service released by Google allows the program to send queries and accept and print the results of Google queries.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.