真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

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

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

創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)網(wǎng)站建設(shè)與網(wǎng)站營銷,提供成都網(wǎng)站設(shè)計、網(wǎng)站制作、網(wǎng)站開發(fā)、seo優(yōu)化、網(wǎng)站排名、互聯(lián)網(wǎng)營銷、成都小程序開發(fā)、公眾號商城、等建站開發(fā),創(chuàng)新互聯(lián)網(wǎng)站建設(shè)策劃專家,為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制解決方案,幫助客戶在新的全球化互聯(lián)網(wǎng)環(huán)境中保持優(yōu)勢。

實例目錄:

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

實現(xiàn)效果:

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

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

具體思路:

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

獲取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();
 /*對和風(fēng)天氣提供的鏈接進(jìn)行連接*/
 connection.connect();
 /*獲取狀態(tài)碼*/
 int recode = connection.getResponseCode();
 BufferedReader bufferedReader = null;
 String json = new String();
 /*如果連接成功*/ 
 if(recode==200) {
 /*對數(shù)據(jù)進(jìn)行讀,并且封裝到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字符串進(jìn)行解析,這里使用谷歌的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 getData(String json) {

 
 
 ArrayList lists = new ArrayList();
 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層:

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 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"?>



 
 
 

 
 
 
 
 

  
 
 
 
 


查詢主頁:

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



 
 " rel="external nofollow" rel="external nofollow" >
 
 My JSP 'index.jsp' starting page
 
 
  
 
 
 
 
 
 
 
 
city:

展示頁面:

<%@ 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+"/";
%>



 
 
 
 My JSP 'display.jsp' starting page
 
 
 
  
 
 
 

 
 
 
   
 
 
   
 

     
 
 
 
 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


網(wǎng)站題目:SpringMVC結(jié)合天氣api實現(xiàn)天氣查詢
URL標(biāo)題:http://weahome.cn/article/gicsdp.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部