现在的位置: 首页 > 综合 > 正文

Jquery+AJAX++获取google+API+图片源

2018年04月13日 ⁄ 综合 ⁄ 共 6691字 ⁄ 字号 评论关闭

Jquery+AJAX  获取google API 图片源:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script type="text/javascript" src="js/jquery-1.6.4.js"></script>
	<script type="text/javascript">
		$(function(){
			$(":button[value=click]").bind("click",function(){
				

			$.post("ImageServlet",
					{name1:$(":text[name=name1]").val()},
					function(data){
						$("#image").empty();
						$.each(data,function(k,v){
							 vs=eval('('+v+')');
							$("#image").append("<image src='"+vs.responseData.results[0].url+"' width=150 height=150 />");
						})
					}
				)
			})
		});
	</script>
  </head>
  
  <body>
    <input type="text" name="name1" /><input type="button" value="click" />
    <div id="image"></div>
    
    <!--  
    		$.getJSON("ImageServlet",{name1:$(":text[name=name1]").val()},function(data){
					$.each(data,function(k,v){
						alert(v);
						

						})
				})		
    
    -->
  </body>
</html>

ImageServlet

package com.wansha.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class ImageServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
		String name1 = request.getParameter("name1");
		List<String> list = new ArrayList<String>();
		String content = "";
		for(int i=0;i<64;i++){
			String queryString = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=large";
			queryString += "&q=" + name1 + "&start=" + i;

			content = NetworkUtil.getInternetJson(queryString);
			System.out.println(content);
			list.add(content);
		}
		System.out.println(list.size());
		Gson gson = new Gson();
		String resultedString = gson.toJson(list);
//		resultedString	= "[{\"responseData\": {\"results\":[{\"GsearchResultClass\":\"GimageSearch\",\"width\":\"462\",\"height\":\"500\",\"imageId\":\"ANd9GcSUE2fiP81fETB1AdQrJvhb9m3CDa-DrDsIGyDHaLjYloqdonxpeR16tgSN\",\"tbWidth\":\"120\",\"tbHeight\":\"130\",\"unescapedUrl\":\"http://www.blogcdn.com/www.engadget.com/media/2007/01/apple-iphone-official-1.jpg\",\"url\":\"http://www.blogcdn.com/www.engadget.com/media/2007/01/apple-iphone-official-1.jpg\",\"visibleUrl\":\"www.engadget.com\",\"title\":\"The Apple \\u003cb\\u003eiPhone\\u003c/b\\u003e -- Engadget\",\"titleNoFormatting\":\"The Apple iPhone -- Engadget\",\"originalContextUrl\":\"http://www.engadget.com/2007/01/09/the-apple-iphone/\",\"content\":\"\\u0026quot;\\u003cb\\u003eiPhone\\u003c/b\\u003e,\\u0026quot; the name the\",\"contentNoFormatting\":\"\\u0026quot;iPhone,\\u0026quot; the name the\",\"tbUrl\":\"http://t2.gstatic.com/images?q\\u003dtbn:ANd9GcSUE2fiP81fETB1AdQrJvhb9m3CDa-DrDsIGyDHaLjYloqdonxpeR16tgSN\"},{\"GsearchResultClass\":\"GimageSearch\",\"width\":\"200\",\"height\":\"267\",\"imageId\":\"ANd9GcR7uibMR_S9f3nu_jWpN0G2fdbkNRjibyAjb_GUd7Osf27uZJrkZMNsdg\",\"tbWidth\":\"85\",\"tbHeight\":\"113\",\"unescapedUrl\":\"http://static.howstuffworks.com/gif/iphone-1a.jpg\",\"url\":\"http://static.howstuffworks.com/gif/iphone-1a.jpg\",\"visibleUrl\":\"electronics.howstuffworks.com\",\"title\":\"HowStuffWorks \\u0026quot;How the \\u003cb\\u003eiPhone\\u003c/b\\u003e Works\\u0026quot;\",\"titleNoFormatting\":\"HowStuffWorks \\u0026quot;How the iPhone Works\\u0026quot;\",\"originalContextUrl\":\"http://electronics.howstuffworks.com/iphone.htm\",\"content\":\"\\u003cb\\u003eiPhone\\u003c/b\\u003e Image Gallery\",\"contentNoFormatting\":\"iPhone Image Gallery\",\"tbUrl\":\"http://t0.gstatic.com/images?q\\u003dtbn:ANd9GcR7uibMR_S9f3nu_jWpN0G2fdbkNRjibyAjb_GUd7Osf27uZJrkZMNsdg\"},{\"GsearchResultClass\":\"GimageSearch\",\"width\":\"503\",\"height\":\"830\",\"imageId\":\"ANd9GcQH8DjQYGla8At7MGLUBGndtGDJ4ME4ZO1bRDhQacBfi4ab4PHK8NuhXzk\",\"tbWidth\":\"87\",\"tbHeight\":\"144\",\"unescapedUrl\":\"http://davidcann.com/iphone/iphone-interface.jpg\",\"url\":\"http://davidcann.com/iphone/iphone-interface.jpg\",\"visibleUrl\":\"davidcann.com\",\"title\":\"\\u003cb\\u003eiPhone\\u003c/b\\u003e Interface in JavaScript\",\"titleNoFormatting\":\"iPhone Interface in JavaScript\",\"originalContextUrl\":\"http://davidcann.com/iphone/\",\"content\":\"\\u003cb\\u003eiPhone\\u0026#39;s\\u003c/b\\u003e Third-Party Apps\",\"contentNoFormatting\":\"iPhone\\u0026#39;s Third-Party Apps\",\"tbUrl\":\"http://t2.gstatic.com/images?q\\u003dtbn:ANd9GcQH8DjQYGla8At7MGLUBGndtGDJ4ME4ZO1bRDhQacBfi4ab4PHK8NuhXzk\"},{\"GsearchResultClass\":\"GimageSearch\",\"width\":\"386\",\"height\":\"701\",\"imageId\":\"ANd9GcTlsWHdWID5NFiSkn8EnhirL6jPBTn3BWFBFyACwpLGOXdpP61HfZamn6o\",\"tbWidth\":\"77\",\"tbHeight\":\"140\",\"unescapedUrl\":\"http://images.macworld.com/images/news/graphics/133988-iphone3g.jpg\",\"url\":\"http://images.macworld.com/images/news/graphics/133988-iphone3g.jpg\",\"visibleUrl\":\"www.macworld.com\",\"title\":\"Review: \\u003cb\\u003eiPhone\\u003c/b\\u003e 3G Review | Phones - Page 1 | Macworld\",\"titleNoFormatting\":\"Review: iPhone 3G Review | Phones - Page 1 | Macworld\",\"originalContextUrl\":\"http://www.macworld.com/article/134482/2008/07/iphone3g_review.html\",\"content\":\"\\u003cb\\u003eiPhone\\u003c/b\\u003e 3G\\u0026#39;s dock connector\",\"contentNoFormatting\":\"iPhone 3G\\u0026#39;s dock connector\",\"tbUrl\":\"http://t3.gstatic.com/images?q\\u003dtbn:ANd9GcTlsWHdWID5NFiSkn8EnhirL6jPBTn3BWFBFyACwpLGOXdpP61HfZamn6o\"}],\"cursor\":{\"pages\":[{\"start\":\"0\",\"label\":1},{\"start\":\"4\",\"label\":2},{\"start\":\"8\",\"label\":3},{\"start\":\"12\",\"label\":4},{\"start\":\"16\",\"label\":5},{\"start\":\"20\",\"label\":6},{\"start\":\"24\",\"label\":7},{\"start\":\"28\",\"label\":8}],\"estimatedResultCount\":\"406000000\",\"currentPageIndex\":0,\"moreResultsUrl\":\"http://www.google.com/images?oe\\u003dutf8\\u0026ie\\u003dutf8\\u0026source\\u003duds\\u0026start\\u003d0\\u0026hl\\u003den\\u0026q\\u003diphone\"}}, \"responseDetails\": null, \"responseStatus\": 200}]";
		PrintWriter pw = response.getWriter();
		response.setContentType("application/json");
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		pw.write(resultedString);
		pw.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}

【上篇】
【下篇】

抱歉!评论已关闭.