最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年05月05日 09:25:14   作者:Coder_py  
這篇文章主要為大家詳細介紹了SpringMVC結合天氣api實現(xiàn)天氣查詢,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

實例目錄:


實現(xiàn)效果:




具體思路:

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

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

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ù)進行讀,并且封裝到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); //創(chuàng)建JsonObject對象
 JsonArray array=object.get("results").getAsJsonArray();//得到json數(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層:

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 />
 
 <!-- 靜態(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>

查詢主頁:

<%@ 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>

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

相關文章

  • 解決Idea項目結構顯示不全問題

    解決Idea項目結構顯示不全問題

    文章描述了在使用IntelliJ IDEA時遇到的問題,并提出了解決方法:關閉IDEA、刪除項目中的.idea文件夾,然后重新打開IDEA導入項目
    2024-11-11
  • SpringBoot整合Kaptcha實現(xiàn)圖片驗證碼加減乘除功能

    SpringBoot整合Kaptcha實現(xiàn)圖片驗證碼加減乘除功能

    在開發(fā)Web應用時,驗證碼是一個常見的功能,它可以幫助我們防止機器人的惡意操作,今天我們將學習如何使用Kaptcha生成圖片驗證碼,并自定義驗證碼內(nèi)容為100以內(nèi)的加減乘除運算,感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • jdk1.8中的for循環(huán)問題記錄

    jdk1.8中的for循環(huán)問題記錄

    這篇文章主要介紹了jdk1.8中的for循環(huán)及jdk1.8 新特性之 forEach 循環(huán)遍歷問題,本文通過實例代碼給大家詳細講解,需要的朋友可以參考下
    2022-11-11
  • SpringBoot+Vue項目新手快速入門指南

    SpringBoot+Vue項目新手快速入門指南

    最近剛剛做了一個基于vue+springboot的系統(tǒng),于是基于這點,對遇到的一些問題進行一些配置的匯總,下面這篇文章主要給大家介紹了關于SpringBoot+Vue項目新手快速入門的相關資料,需要的朋友可以參考下
    2022-06-06
  • ssm開發(fā)使用redis作為緩存的使用步驟

    ssm開發(fā)使用redis作為緩存的使用步驟

    在開發(fā)中經(jīng)常遇到大量的重復的,高并發(fā)的查詢,此時可以使用redis緩存。這篇文章主要介紹了ssm開發(fā)使用redis作為緩存的使用步驟,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Spark集群框架的搭建與入門

    Spark集群框架的搭建與入門

    Spark是專為大規(guī)模數(shù)據(jù)處理而設計的,基于內(nèi)存快速通用,可擴展的集群計算引擎,實現(xiàn)了高效的DAG執(zhí)行引擎,可以通過基于內(nèi)存來高效處理數(shù)據(jù)流,運算速度相比于MapReduce得到了顯著的提高。
    2021-06-06
  • java中Path和ClassPath用法比較

    java中Path和ClassPath用法比較

    在本篇文章里小編給大家分享了關于java中Path和ClassPath用法比較內(nèi)容,有需要的朋友們學習下。
    2019-01-01
  • Java入門教程--帶包的類如何編譯與運行

    Java入門教程--帶包的類如何編譯與運行

    我們一般都是通過IDE(如Eclipse、Intellij Idea,STS等)來開發(fā),調(diào)試java項目。在不借助IDE的情況下,如何編譯、運行Java程序。打包編譯時,會自動創(chuàng)建包目錄,不需要自己新建包名文件夾。
    2022-12-12
  • zookeeper實戰(zhàn)之實現(xiàn)分布式鎖的方法

    zookeeper實戰(zhàn)之實現(xiàn)分布式鎖的方法

    Zookeeper實現(xiàn)分布式鎖比Redis簡單,Zookeeper有一個特性,多個線程在Zookeeper里創(chuàng)建同一個節(jié)點時,只有一個線程執(zhí)行成功,Zookeeper主要是利用臨時有序節(jié)點這一特性實現(xiàn)分布式鎖,感興趣的朋友跟隨小編一起學習吧
    2022-11-11
  • Spring Boot 實現(xiàn)敏感詞及特殊字符過濾處理

    Spring Boot 實現(xiàn)敏感詞及特殊字符過濾處理

    這篇文章主要介紹了SpringBoot實現(xiàn)敏感詞及特殊字符過濾處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評論

内丘县| 霍邱县| 门头沟区| 理塘县| 商城县| 无锡市| 盐池县| 竹山县| 康乐县| 安平县| 济南市| 河间市| 农安县| 恩平市| 黔江区| 巴里| 民和| 莆田市| 班戈县| 深水埗区| 偏关县| 怀远县| 南雄市| 永清县| 邯郸县| 清流县| 睢宁县| 泰宁县| 宝坻区| 泾阳县| 江川县| 宜都市| 拜泉县| 旌德县| 闽清县| 武邑县| 孝感市| 芜湖县| 和顺县| 榕江县| 麻城市|