欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

本實例實現(xiàn)在jsp頁面實現(xiàn)查詢全國城市天氣預報的功能,供大家參考,具體內容如下

實例目錄:


實現(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>

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

相關文章

  • 談談Java中的守護線程與普通線程

    談談Java中的守護線程與普通線程

    這篇文章主要介紹了Java中的守護線程與普通線程,幫助大家更好的理解和學習Java 多線程,感興趣的朋友可以了解下
    2020-09-09
  • 詳解如何全注解方式構建SpringMVC項目

    詳解如何全注解方式構建SpringMVC項目

    這篇文章主要介紹了詳解如何全注解方式構建SpringMVC項目,利用Eclipse構建SpringMVC項目,非常具有實用價值,需要的朋友可以參考下
    2018-10-10
  • java實現(xiàn)簡單圖書管理系統(tǒng)

    java實現(xiàn)簡單圖書管理系統(tǒng)

    這篇文章主要為大家詳細介紹了java實現(xiàn)簡單圖書管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 淺談Spring如何解決循環(huán)依賴的問題

    淺談Spring如何解決循環(huán)依賴的問題

    這篇文章主要介紹了淺談Spring如何解決循環(huán)依賴的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • java擴展Hibernate注解支持java8新時間類型

    java擴展Hibernate注解支持java8新時間類型

    這篇文章主要介紹了java擴展Hibernate注解支持java8新時間類型,需要的朋友可以參考下
    2014-04-04
  • 詳解SpringBoot結合swagger2快速生成簡單的接口文檔

    詳解SpringBoot結合swagger2快速生成簡單的接口文檔

    這篇文章主要介紹了詳解SpringBoot結合swagger2快速生成簡單的接口文檔,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • 解決Spring國際化文案占位符失效問題的方法

    解決Spring國際化文案占位符失效問題的方法

    本篇文章主要介紹了解決Spring國際化文案占位符失效問題的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • IDEA讓包分層顯示的實現(xiàn)方式

    IDEA讓包分層顯示的實現(xiàn)方式

    這篇文章主要介紹了IDEA讓包分層顯示的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • JAVA線程池監(jiān)控以及動態(tài)調整示例詳解

    JAVA線程池監(jiān)控以及動態(tài)調整示例詳解

    這篇文章主要為大家介紹了JAVA線程池監(jiān)控以及動態(tài)調整示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • 淺談Java的WeakHashMap源碼

    淺談Java的WeakHashMap源碼

    這篇文章主要介紹了淺談Java的WeakHashMap源碼,WeakHashMap,從名字可以看出它是某種?Map,它的特殊之處在于?WeakHashMap?里的entry可能會被GC自動刪除,即使程序員沒有調用remove()或者clear()方法,需要的朋友可以參考下
    2023-09-09

最新評論