ajax调用google API接口获取搜索结果.docx
-
资源ID:4882816
资源大小:134.07KB
全文页数:12页
- 资源格式: DOCX
下载积分:15金币
友情提示
2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
|
ajax调用google API接口获取搜索结果.docx
ajax search 功能参考:httD:/AJAX Search API一简单的Google API搜索例子首先看- 一个Java代码的例子需要两个jar的支持commons-httpclient.jar commons-codec-1.3.jarview plaincopy t。clipboardprint?import java.io.UnsupportedEncodingException;import .URLEncoder;import mons.httpcIient.DefaultHttpMethodRetryHandler;import mons.httpcIient.HttpCIient;import mons.httpclient. HttpStatus;import mons.httpclient.methods.GetMethod;import mons.httpclient.pa rams. HttpMethodParams;import org.json.JSONArray;import org.json.JSONObject;public class GoogleQuery (public GoogleQuery() throws UnsupportedEncodingException String query = URLEncoder.encode("AOP 示例+site: "UTF-8");/站内搜索 /String query = URLEncoder.encode("AOP 示例","UTF-8");for (int i = 0; i < 3; i+) makeQuery("+ i * 8 + "&rsz=large&v=1.0&q=" + query);private void makeQuery(String query) try HttpClient httpClient = new HttpClient();GetMethod getMethod = new GetMethod(query);httpClient.executeMethod(getMethod);getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler();int statusCode = httpClient.executeMethod(getMethod);if (statusCode != HttpStatus.SC_OK) System.err.println("Method failed:"+ getMethod.getStatusLine();byte responseBody = getMethod.getResponseBody();String response = new String(responseBody, "UTF-8");JSONObject json = new JSONObject(response);System.out.println("Total results ="+ json.getJSONObject("responseData").getJSONObject("cursor").getString("estimatedResultCount");JSONArray ja = json.getJSONObject("responseData").getJSONArray( "results");System.out.println(" Results:");for (int i = 0; i < ja.length(); i+) (JSONObject j = ja.getJSONObject(i);System.out.printing.getString("titleNoFormatting");System.out.printing.getStringC'url");System.out.printing.get("content"); catch (Exception e) (System.err.println("Something went wrong.");e.printStackTrace();System.out.println("");public static void main(String args) (trynew GoogleQueryO; catch (UnsupportedEncodingException e) (e.printStackTrace();再来一个js的例子view plaincopy to clipboardprint?<!DOCTYPE html PUBLIC "-/W3C/DTD XHTML 1.0 Strict/EN" "http:/www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd><html xmlns="http:/www.w3.org/1999/xhtml><head><title>My Google AJAX Search API Application</title><mce:script src=" 申 请 的 key" mce_src=" 申 请 的 key" type="text/javascript'></mce:script><mce:script language="Javascript" type="text/javascript'><!-google.load("search", "1");function OnLoad() (/ Create a search controlvar searchControl = new google.search.SearchControl();/ Add in a full set of searchersvar localSearch = new google.search.LocalSearch();searchControl.addSearcher(localSearch);searchControl.addSearcher(new google.search.WebSearch();searchControl.addSearcher(new google.search.VideoSearch();searchControl.addSearcher(new google.search.BlogSearch();/ Set the Local Search center point/localSearch.setCenterPoint("New York, NY");/ Tell the searcher to draw itself and tell it where to attachsearchControl.draw(document.getElementById("searchcontrol");/ Execute an inital searchsearchControl.execute("java");google.setOnLoadCallback(OnLoad);/ -></mce:script></head><body><div id="searchcontrol">Loading.</div></body></html>补充:有问题的小程序,可以参考:“google搜出来的结果远不是4个,但为什么我用firebug调试的时候发现searcher.results.length的值为4呢?” function searchComplete(searchControl, searcher) / Do whatever you want with the resultsfor (var i=0; i < searcher.results.length; i+) function OnLoad() var searchControl = new google.search.SearchControl();var webSearch = new google.search.WebSearch();searchControl.addSearcher(webSearch);searchControl.draw(document.getElementById("searchcontrol");searchControl.setSearchCompleteCallback(this, searchComplete);searchControl.execute('Google')google.setOnLoadCallback(OnLoad);jquery google API AjaxAPI 库调用Google API的代码库,可以有效的减少自己站上的流量。比如Jquery, gzip后有19K,调用Google Api,不仅省去了 流量,如果大家的网站都这么做的话,由于缓存原因,很多网站用到的话,就不需要重复加载了,非常节省资源,和加载时间。采用Ajax动态加载google的API中jquery库的代码是这样的:view plaincopy to clipboardprint?1. <script type="text/javascript" src=".tw/jsapi"></script>2. <script type="text/javascript">3. google.load("jquery", "1.3.2");/加载 jQuery 1.3.24. </script><script src=" type="text/javascript"></script>之所以采用 .tw (这里也可以更换成其他域名如google.de,google.jp之类的)是因为 有时会不稳定,就会导致加载此代码的页面打不开。实际上如果你只用到jquery的话,根据firebug (火狐下插件)给出的实际地址, 也可以直接向下面这样引入jquery库。view plaincopy to clipboardprint?| 1. <script type="text/javascript" src="希望对需要的朋友有用。:)引自 *使用 Google Ajax Search API 的准备工作 使用 Google Ajax Search API一个博客的例子 有一些检索的知识*此 API 如何使用 IntelliJIDEA 开发平台,name: Daniel ; key: 22457-MWPIW-INDLX-2PU2X-WBHSX-C4YK0掌握 Ajax,第 10 部分:使用 JSON 进行数据传输 *Ajax 早期讲解 06 年,很全 一个此 API 的详细讲解 Sign-up for an API KeyThanks for Signing up for a Google API key!Your key is:ABQIAAAA6mcpLt_42oc9byaXDAEQ4RT1xtEyFQOWuDnRzwI0AEsLsT5oSxRef5xHJc6ynRfHTZtAuNw5zuUM9QThis key is good for all URLs in this directory:.hk/Here is an example web page to get you started:<!DOCTYPE html PUBLIC "-/W3C/DTD XHTML 1.0 Strict/EN" "http:/www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http:/www.w3.org/1999/xhtml"><head><title>My Google API Application</title><script src="5zuUM9Q" type="text/javascript"></script><script language="Javascript" type="text/javascript">/<!CDATAgoogle.load("search","1");function OnLoad() / Create a search controlvar searchControl = new google.search.SearchControl();/ Add in a full set of searchersvar localSearch = new google.search.LocalSearch();searchControl.addSearcher(localSearch);searchControl.addSearcher(new google.search.WebSearch();searchControl.addSearcher(new google.search.VideoSearch();searchControl.addSearcher(new google.search.BlogSearch();/ Set the Local Search center point localSearch.setCenterPoint("New York, NY");/ Tell the searcher to draw itself and tell it where to attach searchControl.draw(document.getElementById("searchcontrol");/ Execute an inital search searchControl.execute("Google");google.setOnLoadCallback(OnLoad);/></script></head><body><div id="searchcontrol">Loading.</div></body></html>Check out the API documentation for more information.ajax search 功能参考:http:httD:/AJAX Search API