ajax search功能参考:so.csdn/search?t=thread&q=ajax%20search
le/intl/zh-CN/apis/loader/
le/intl/zh-CN/apis/websearch/docs/
AJAX Search API
一简单的Google API搜索例子 |
首先看一个Java代码的例子 需要两个jar的支持 commons-httpclient.jar commons-codec-1.3.jar view plaincopy to clipboardprint? import java.io.UnsupportedEncodingException; import java.URLEncoder; import org.apachemons.httpclient.DefaultHttpMethodRetryHandler; import org.apachemons.httpclient.HttpClient; import org.apachemons.httpclient.HttpStatus; import org.hods.GetMethod; import org.apachemons.httpclient.params.HttpMethodParams; import org.json.JSONArray; import org.json.JSONObject; public class GoogleQuery { public GoogleQuery() throws UnsupportedEncodingException { String query =&de("AOP示例+site:blog.csdn/zhjb1025", "UTF-8");//站内搜索 //String query =&de("AOP示例", "UTF-8"); for (int i = 0; i < 3; i++) { makeQuery("leapis/ajax/services/search/web?start=" + i * 8 + "&rsz=large&v=1.0&q=" + query); } } private void makeQuery(String query) { try { HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(query); uteMethod(getMethod); Params().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); int statusCode =&uteMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { println("Method failed: " +&StatusLine()); } byte[] responseBody =&ResponseBody(); String response = new String(responseBody, "UTF-8"); JSONObject json = new JSONObject(response); System.out.println("Total results = " +&JSONObject("responseData") .getJSONObject("cursor").getString( "estimatedResultCount")); JSONArray ja =&JSONObject("responseData").getJSONArray( "results"); System.out.println(" Results:"); for (int i = 0; i < ja.length(); i++) { JSONObject j =&JSONObject(i); System.out.String("titleNoFormatting")); System.out.String("url")); jquery ajax例子 System.out.("content")); } } catch (Exception e) { println("Something went&"); e.printStackTrace(); } System.out.println("--------------------------------------------"); } public static void main(String args[]) { try { new GoogleQuery(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } 再来一个js的例子 view plaincopy to clipboardprint? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="/1999/xhtml"> <head> <title>My Google AJAX Search API Application</title> <mce:script src="le/jsapi?key=你申请的key" mce_src="le/jsapi?key=你申请的key" type="text/javascript"></mce:script> <mce:script language="Javascript" type="text/javascript"><!-- google.load("search", "1"); function OnLoad() { // Create a search control var searchControl = new google.search.SearchControl(); // Add in a full set of searchers var 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.ElementById("searchcontrol")); // Execute an inital search ute("java"); } google.setOnLoadCallback(OnLoad); // --></mce:script> </head> <body> <div id="searchcontrol"></div> </body> </html> |
补充:
有问题的小程序,可以参考:“google搜出来的结果远不是4个,但为什么我用firebug调试的时候发现sults.length的值为4呢?”
function searchComplete(searchControl, searcher) {
for (var i=0; i < sults.length; i++) {
// Do whatever you want with the results
}
}
function OnLoad() {
var searchControl = new google.search.SearchControl();
var webSearch = new google.search.WebSearch();
searchControl.addSearcher(webSearch);
searchControl.ElementById("searchcontrol"));
searchControl.setSearchCompleteCallback(this, searchComplete);
ute('Google')
}
google.setOnLoadCallback(OnLoad);
jquery google API AjaxAPI 库
调用Google API的代码库,可以有效的减少自己站上的流量。比如Jquery,gzip后有19K,调用Google Api,不仅省去了流量,如果大家的网站都这么做的话,由于缓存原因,很多网站用到的话,就不需要重复加载了,非常节省资源,和加载时间。
采用Ajax动态加载google的API中jquery库的代码是这样的:
view plaincopy to clipboardprint?
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论