栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC结合天气api实现天气查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC结合天气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) {
 
 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 getData(String json) {

 
 
 ArrayList lists = new ArrayList();
 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 lists = jsonUtilImpl.getData(data);
 model.addAttribute("lists",lists);
 return "display";
 
 }
 @RequestMapping("/fff")
 public String fff() {
 return "a";
 }
}

springMVC配置文件:





 
 
 

 
 
 
 
 

  
 
 
 
 


查询主页:

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



 
 
 
 My JSP 'index.jsp' starting page
 
 
  
 
 
 
 
 
 
 
 
 
 
 
 


展示页面:

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

 
 
 
   
 
 
   
 

     
 
 
 
 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/146359.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号