SpringMVC结合天气api实现天气查询
程序员文章站
2024-02-23 16:27:22
本实例实现在jsp页面实现查询全国城市天气预报的功能,供大家参考,具体内容如下
实例目录:
实现效果:
具体思路:
从和风天气api那里...
本实例实现在jsp页面实现查询全国城市天气预报的功能,供大家参考,具体内容如下
实例目录:
实现效果:
具体思路:
从和风天气api那里取得具体城市的api接口,获取json数据,再对json数据进行解析。
获取json数据:
package com.util; import java.io.bufferedreader; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.nio.bytebuffer; import java.nio.channels.filechannel; public class netutilimpl implements netutil{ @override public string getjson(string url) throws ioexception{ httpurlconnection connection = null; url url2 = new url(url); connection=(httpurlconnection) url2.openconnection(); /*对和风天气提供的链接进行连接*/ connection.connect(); /*获取状态码*/ int recode = connection.getresponsecode(); bufferedreader bufferedreader = null; string json = new string(); /*如果连接成功*/ if(recode==200) { /*对数据进行读,并且封装到json这个字符串,并且返回json*/ inputstream inputstream = connection.getinputstream(); bufferedreader=new bufferedreader(new inputstreamreader(inputstream,"utf-8")); string string = null; while ((string=bufferedreader.readline())!=null) { json=json+string; bytebuffer buffer = bytebuffer.wrap(new string(string).getbytes("utf-8")); } } return json; } }
对json字符串进行解析,这里使用谷歌的gson工具包:
package com.util; import java.io.filereader; import java.util.arraylist; import java.util.list; import com.google.gson.jsonarray; import com.google.gson.jsonobject; import com.google.gson.jsonparser; public class jsonutilimpl implements jsonutil{ @override public list<string> getdata(string json) { arraylist<string> lists = new arraylist<string>(); jsonparser jsonparser = new jsonparser();//json解析器 jsonobject object=(jsonobject) jsonparser.parse(json); //创建jsonobject对象 jsonarray array=object.get("results").getasjsonarray();//得到json数组 jsonobject sjsonobject = array.get(0).getasjsonobject();//按索引得到其中具体数据 jsonobject location = sjsonobject.get("location").getasjsonobject(); jsonobject now = sjsonobject.get("now").getasjsonobject(); lists.add(location.get("name").getasstring()); lists.add(now.get("text").getasstring()); lists.add(now.get("temperature").getasstring()); // lists.add(now.get("humidity").getasstring()); // lists.add(now.get("wind_speed").getasstring()); return lists; } }
完整代码:
controller层:
package com.web; import java.io.ioexception; import java.util.list; import javax.annotation.resource; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; import org.w3c.dom.ls.lsexception; import com.google.common.collect.lists; import com.sun.org.apache.bcel.internal.generic.new; import com.util.jsonutil; import com.util.jsonutilimpl; import com.util.netutil; import com.util.netutilimpl; @controller @requestmapping("/wea") public class forest { netutilimpl netutilimpl = new netutilimpl(); jsonutilimpl jsonutilimpl = new jsonutilimpl(); @requestmapping("/forest") public string forest(string city,model model) throws ioexception { string url = "https://api.seniverse.com/v3/weather/now.json?key=mtpmwyecaphmrzwc&location="+city+"&language=zh-hans&unit=c"; string data = netutilimpl.getjson(url); list<string> lists = jsonutilimpl.getdata(data); model.addattribute("lists",lists); return "display"; } @requestmapping("/fff") public string fff() { return "a"; } }
springmvc配置文件:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 注解扫描包 --> <context:component-scan base-package="com.web" /> <!-- 开启注解 --> <mvc:annotation-driven /> <!-- 静态资源(js/image)的访问 --> <mvc:resources location="/js/" mapping="/js/**"/> <!-- 定义视图解析器 --> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
查询主页:
<%@ 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%>" rel="external nofollow" rel="external nofollow" > <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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" > --> </head> <body> <form action="/myweather/wea/forest"> city: <input type="text" name="city"> <input type="submit" value="提交"> </form> </body> </html>
展示页面:
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% 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%>" rel="external nofollow" rel="external nofollow" > <title>my jsp 'display.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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" > --> </head> <body> <c:if test="${!empty lists }"> <c:foreach items="${lists}" var="lists"> <c:out value="${lists}"></c:out> </c:foreach> </c:if> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: MyBatis插入数据返回主键的介绍
下一篇: 用JSP实现的一个日历程序