ajax获取当前城市天气情况
2017-08-23 15:22
417 查看
ajax获取当前城市天气情况
最进项目用到了天气的功能,经过不懈的努力终于有了一点收获,特此发上来与大家共享js脚本
<script type="text/javascript"> $(document).ready(function(){ //得到当前地址 $.getScript("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js",function(){ //天气请求ajax $.ajax({ url:'customer/weather.htm', //发送路径 type:"post", //发送方式 async:true, //true异步 false同步 data:{ //参数类型json data address:remote_ip_info.city }, dataType:"text", //返回类型 success:function(data){ document.getElementById("weather").innerHTML=data; } }); }); }); $("#weatherButton").click(function(){ //天气请求ajax $.ajax({ url:'customer/weather.htm', //发送路径 type:"post", //发送方式 async:true, //true异步 false同步 data:{ //参数类型json data weather address:"苏州" }, dataType:"text", //返回类型 success:function(data){ alert(data); } }); }); </script>
Controller
@RequestMapping("weather") public void weather(HttpSession session,HttpServletResponse response,@RequestParam("address") String address) throws Exception{ String city_url = URLEncoder.encode(address, "GBK"); // 原GBK 定义需要获取天气信息的城市和编码格式 String link="http://php.weather.sina.com.cn/xml.php?city="+city_url+"&password=DJOYnieT8234jlsK&day=0"; //气象台接口地址 URL url; //声明地址对象 try { url = new URL(link); //生成url WeatherUtil parser = new WeatherUtil(url); //将url交给工具类处理返回天气信息 String[] nodes = {"city","status1","temperature1","status2","temperature2"}; //定义一个接受天气信息的字符串数组 Map<String, String> map = parser.getValue(nodes); //天气信息转换为Map对象 String weather=map.get(nodes[0])+" 今天白天:"+map.get(nodes[1])+" 最高温度:"+map.get(nodes[2])+"℃ 今天夜间:"+map.get(nodes[3])+" 最低温度:"+map.get(nodes[4])+"℃ "; //便利数据生成信息字符串 System.out.println(address+":"+weather); response.setCharacterEncoding("utf-8"); response.getWriter().println(weather); //使用AJAX将天气信息返回 session.setAttribute("weather", weather); //防止丢失将天气信息同时放入session中 } catch (MalformedURLException e) { e.printStackTrace(); } }
工具类
package com.ambow.invoic.utils; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class WeatherUtil { InputStream inStream; Element root; public InputStream getInStream() { return inStream; } public void setInStream(InputStream inStream) { this.inStream = inStream; } public Element getRoot() { return root; } public void setRoot(Element root) { this.root = root; } public WeatherUtil() { } /** * 通过输入流来获取新浪接口信息 * @param inStream */ public WeatherUtil(InputStream inStream) { if (inStream != null) { this.inStream = inStream; DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); try { DocumentBuilder domBuilder = domfac.newDocumentBuilder(); Document doc = domBuilder.parse(inStream); root = doc.getDocumentElement(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } public WeatherUtil(String path) { InputStream inStream = null; try { inStream = new FileInputStream(path); } catch (FileNotFoundException e1) { e1.printStackTrace(); } if (inStream != null) { this.inStream = inStream; DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); try { DocumentBuilder domBuilder = domfac.newDocumentBuilder(); Document doc = domBuilder.parse(inStream); root = doc.getDocumentElement(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } public WeatherUtil(URL url) { InputStream inStream = null; try { inStream = url.openStream(); } catch (IOException e1) { e1.printStackTrace(); } if (inStream != null) { this.inStream = inStream; DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); try { DocumentBuilder domBuilder = domfac.newDocumentBuilder(); Document doc = domBuilder.parse(inStream); root = doc.getDocumentElement(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @param nodes * @return 单个节点多个值以分号分隔 */ public Map<String, String> getValue(String[] nodes) { if (inStream == null || root==null) { return null; } Map<String, String> map = new HashMap<String, String>(); // 初始化每个节点的值为null for (int i = 0; i < nodes.length; i++) { map.put(nodes[i], null); } // 遍历第一节点 NodeList topNodes = root.getChildNodes(); if (topNodes != null) { for (int i = 0; i < topNodes.getLength(); i++) { Node book = topNodes.item(i); if (book.getNodeType() == Node.ELEMENT_NODE) { for (int j = 0; j < nodes.length; j++) { for (Node node = book.getFirstChild(); node != null; node = node.getNextSibling()) { if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeName().equals(nodes[j])) { String val = node.getTextContent(); String temp = map.get(nodes[j]); if (temp != null && !temp.equals("")) { temp = temp + ";" + val; } else { temp = val; } map.put(nodes[j], temp); } } } } } } } return map; } }
相关文章推荐
- java调用webservice获取指定城市天气情况的例子
- android webservice获取城市天气情况
- flex webservice获取城市的天气情况
- js获取当前城市,并根据当前城市获得当地天气
- Soap请求获取指定城市的天气情况并在天地图API Map中显示
- Unity获取当前城市天气
- python获取城市天气情况案例
- android webservice获取城市天气情况
- 在安卓中获取一个城市的天气情况
- Ajax 通过城市名获取数据(全国天气预报API)
- [一天一项目]获取当前天气——获取某个地区当前的天气情况。
- JavaScript~ajax~城市列表获取天气
- [一天一项目]获取当前天气——获取某个地区当前的天气情况。
- Jquery获取当前城市的天气信息
- android webservice获取城市天气情况
- 想在自己的android应用中获得当天的天气情况,这该怎么做呢?不用担心。中国国家气象局提供了获取所在城市天气预报信息接口。通过这个接口,我们就可以获取天气信息了。
- Android使用"中国天气网"API数据通过城市名称获取天气情况
- WPF实例:通过WebServices获取城市的天气情况
- android webservice获取城市天气情况
- Android使用"中国天气网"API数据通过城市名称获取天气情况