一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringMVC結(jié)合天氣api實現(xiàn)天氣查詢

SpringMVC結(jié)合天氣api實現(xiàn)天氣查詢

2020-09-24 15:51Coder_py Java教程

這篇文章主要為大家詳細介紹了SpringMVC結(jié)合天氣api實現(xiàn)天氣查詢,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本實例實現(xiàn)在jsp頁面實現(xiàn)查詢?nèi)珖鞘刑鞖忸A(yù)報的功能,供大家參考,具體內(nèi)容如下

實例目錄:

SpringMVC結(jié)合天氣api實現(xiàn)天氣查詢

實現(xiàn)效果:

SpringMVC結(jié)合天氣api實現(xiàn)天氣查詢
 

SpringMVC結(jié)合天氣api實現(xiàn)天氣查詢

具體思路:

從和風天氣api那里取得具體城市的api接口,獲取json數(shù)據(jù),再對json數(shù)據(jù)進行解析。

獲取json數(shù)據(jù):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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();
 /*獲取狀態(tài)碼*/
 int recode = connection.getResponseCode();
 BufferedReader bufferedReader = null;
 String json = new String();
 /*如果連接成功*/
 if(recode==200) {
 /*對數(shù)據(jù)進行讀,并且封裝到j(luò)son這個字符串,并且返回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工具包:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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); //創(chuàng)建JsonObject對象
 JsonArray array=object.get("results").getAsJsonArray();//得到j(luò)son數(shù)組
 JsonObject sJsonObject = array.get(0).getAsJsonObject();//按索引得到其中具體數(shù)據(jù)
 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層:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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 />
 
 <!-- 靜態(tài)資源(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>

查詢主頁:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<%@ 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>

展示頁面:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<%@ 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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91嫩草国产在线观看免费 | 手机跑分排行最新排名 | 小sao货水好多真紧h的视频 | 青青草99久久精品国产综合 | 国产精品网站在线观看 | 99精品免费观看 | 乳色吐息讲的是什么 | 免费观看a毛片一区二区不卡 | 99久久精品国产片久人 | 日本免费一区二区三区a区 日本免费三片在线观看 | 王雨纯 羞羞 | 校花在公车上被内射好舒服 | 19+韩国女主播激情vip视频在线 | 日本午夜大片免费观看视频 | 国产精品青青青高清在线 | 欧美高清一区 | 亚裔maricahase和黑人 | 媳妇和公公小说 | 国产第一页无线好源 | 青柠影院在线观看免费完整版1 | 国产精品亚洲综合久久 | 亚洲男人天堂网站 | 日本一区免费观看 | 青久草视频| 无人在线视频高清免费观看动漫 | 男女肉粗暴进来下面好紧 | 男人午夜免费视频 | 11 13加污女qq看他下面 | sese在线| 国产在线一区二区视频 | 亚洲天堂影院 | 男人扒开 | 国产91对白在线观看 | 日本 在线观看 | 男同志gays| 国产免费美女视频 | 香蕉国产精品偷在线播放 | 美女曰逼视频 | 色先锋 影音先锋a 资源站 | 波多 在线播放 | 欧美同志video 在线观看 |