Use Google's Web Service

xiaoxiao2021-03-06  53

First, introduction

The Google search engine provides SOAP-based Web Service. This means that different development languages ​​can use this service. In addition, Google provides a set of Java API interfaces for accessing Web Serivce, which makes the development of Web Serivce. The process of Google Searching features is very easy. Developers can embed Google's search capabilities into their applications. This article will show how to use these Java APIs, and how to use Google's Web Service.

At present, Google's API is still in a beta test phase, so there are some restrictions on the API. For example, using these APIs need to apply for an account. For free applications, in order to prevent improper use of developers, limit each account, you can only query 1000 times a day. At present, Google has not yet proposed formal operational models and charging methods.

Google API currently provides three services, as follows:

1) Search service. The user submits a search content request. The Google server will find more than 2 billion web content, and will return to the user to the user's demand. Generally, this processing only takes a few seconds.

2) Cache Service (Cache). The user submits a URL, and the Google server will return the searcher to the latest record of the URL. In this way, users can easily reuse Google's search results.

3) Spell check. This feature is used to check if the user's query request is valid. The user submits an inspection content, the Google server will return a valid, closest to the query request returned, and the query request will comply with Google's query rules.

Second, the relevant preparation work

In order to develop Java-based programs, we need to work as follows.

1) Establish a Java development environment, Java SDK 1.3.1 or updated version. Related address http: .// java.sun.com/j2se

2) Download the Google API's JAR file, Googleapi.jar. Developers can download at http://www.google.com/apis.

3) Apply for Google access account, current Google supports free application, users need to use an Email account to get new accounts. Note that the current free account supports 1000 maximum queries per day. Application Address: http://www.google.com/apis.

4) (Optional) If the user needs to send SOAP requests directly through the Java program, the developer needs to download the relevant Java package, JAXM. Download address: http://java.sun.com/xml

Third, SOAP use

Google provides SOAP-based Web Service, so users can submit SOAP query requests to Google Server, and then the Google server will process these requests and return the results of the SOAP format. Here is a query request and query results.

Query: For example, there is a query request, the request type is a search, the query is "World Cup", the account is "123456789", as follows for the query.

XMLns: soap-env = "http://schemas.xmlsoap.org/soap/envelop/"

XMLns: xsi = "http://www.w3.org/1999/xmlschema-instance"

XMLns: xsd = "http://www.w3.org/1999/xmlschema">

SOAP-ENV: EncodingStyle =

"http://schemas.xmlsoap.org/soap/encoding/">

123456789

"World CUP"

0

10

True

Latin1

Latin1

Query results: If we perform the above query, you can get the following query results. Among them, the results of the query have a total of about 266,000 records, and the usage time is 0.125012 seconds. The ITEM mark represents a result of a query, in the first query result, the website's URL is http://www.fifaworldcup.com. This example only lists a query result.

XMLns: xsi = "http://www.w3.org/1999/xmlschema-instance"

XMLns: xsd = "http://www.w3.org/1999/xmlschema">

XMLns: ns1 = "URN: Googlesearch"

SOAP-ENV: EncodingStyle = "http://schemas.xmlsoap.org/soap/encoding/">

false

2660000

XMLns: ns2 = "http://schemas.xmlsoap.org/soap/encoding/"

XSI: Type = "NS2: Array"

NS2: ArrayType = "NS1: DirectoryCategory [0]">

0.125012

XMLns: ns3 = "http://schemas.xmlsoap.org/soap/encoding/"

XSI: Type = "NS3: Array" NS3: ArrayType = "NS1: ResulTelement [10]">

10k

True

The Official Site from FIFA, Made by Yahoo. Daily News Updates and Loads of Relevant Information.

http://www.fifaworldcup.com

</p> <p>2002 FIFA <B> World </ b> b> CUP </ b> (tm)</p> <p></ title></p> <p></ item></p> <p>...</p> <p></ resultelements></p> <p><endIndex xsi: type = "xsd: int"> 10 </ endindex></p> <p><searchtips XSI: Type = "xsd: string" /></p> <p><searchcomments xsi: type = "xsd: string" /> <startIndex xsi: type = "xsd: int"> 1 </ startindex></p> <p><Estimateisexact XSI: Type = "XSD: Boolean> False </ Estimateisexact></p> <p><searchQuery XSI: Type = "xsd: string"></p> <p>Science fiction</p> <p></ searchQuery></p> <p></ returnide></p> <p></ ns1: DogoogleSearchResponse></p> <p></ Soap-env: body></p> <p></ Soap-env: envelope></p> <p>Fourth, use Google API</p> <p>Google provides a Java-based API to directly access the Google Server directly to improve the development efficiency of developers. The API wraps Google's Web Service, which is more convenient than Web Service. The API package mainly includes the following classes:</p> <p>Googlesearch: This class encapsulates access to Google Servers, supports search and cache (cache) features.</p> <p>GooglesearchDirectoryCategory: Represents a classification directory in Google</p> <p>Googlesearch Fault: This class is an Exception of an subclass for indicating an error used by the API.</p> <p>GooglesearchResult: This class encapsulates the results of the search.</p> <p>GooglesearchResulTelement: This class represents each record in the search results.</p> <p>The following is an example source code for a simple query, the query request is "World Cup", the query account is "123456789". This example will print the query results. If the user needs to parse the query result, you can use GooglesearchResult and GooglesearchResulTelement classes.</p> <p>Import com.google.soap.Search. *;</p> <p>Import java.io. *;</p> <p>Public class googleapitest {</p> <p>Public static void main (String [] args) {</p> <p>String ClientKey = "123456789";</p> <p>String query = "Word Cup";</p> <p>// Create a Google Search Object, Setur Authorization Key</p> <p>Googlesearch s = new googlesearch ();</p> <p>S.SetKey (ClientKey);</p> <p>Try {</p> <p>S.SetQueryString (Query);</p> <p>GooglesearchResult r = s.dosearch ();</p> <p>System.out.Println ("Google Search Results) T.toString ());</p> <p>} catch (goglesearchfault f) {</p> <p>System.out.println ("THE CALL TO The Google Web Apis Failed:" f.toString ());</p> <p>}</p> <p>}</p> <p>}</p> <p>Five, summary</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-110265.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="110265" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.040</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '24l94T0_2Fbpo6fV4KTWJjR7yRniAExnyxVdwO5uof0uzrQdm6o_2FgLTGYaISldIUvhX0g6aPMAY1ANAfvNjYghog_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>