欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > DOCX文档下载  

    ajax调用google API接口获取搜索结果.docx

    • 资源ID:4882816       资源大小:134.07KB        全文页数:12页
    • 资源格式: DOCX        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    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

    注意事项

    本文(ajax调用google API接口获取搜索结果.docx)为本站会员(牧羊曲112)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开