[一天一项目]获取当前天气——获取某个地区当前的天气情况。
2017-08-03 12:00
453 查看
获取当前天气,用的新浪的api获取天气情况,只是读取了天气和温度信息,其他可以自行添加。
只是简单的实现了功能 并没有做成web项目,只是做了java。package com.project.Weather; /** * java获取新浪天气预报代码 */ 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; /** * 解析xml文档,包括本地文档和url * */ public class GetWeather { 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 GetWeather() { } /** * 通过输入流来获取新浪接口信息 * @param inStream */ public GetWeather(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 GetWeather(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 GetWeather(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; } public static void main(String[] args) throws UnsupportedEncodingException { System.out.println("请输入您需要查询的地点:"); Scanner input = new Scanner(System.in); String city = input.next(); String city_url = URLEncoder.encode(city, "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); GetWeather parser = new GetWeather(url); String[] nodes = {"city","status1","temperature1","status2","temperature2"}; Map<String, String> map = parser.getValue(nodes); System.out.println(map.get(nodes[0])+" 今天白天:"+map.get(nodes[1])+" 最高温度:"+map.get(nodes[2])+"℃ 今天夜间:"+map.get(nodes[3])+" 最低温度:"+map.get(nodes[4])+"℃ "); } catch (MalformedURLException e) { e.printStackTrace(); } } }
解决方法有许多,如果有更多更好的方法,希望可以告知。一起交流成长,谢谢!
代码合集:https://github.com/FanceTsui/Project.git
——Fance Tsui
相关文章推荐
- [一天一项目]获取当前天气——获取某个地区当前的天气情况。
- 实践项目二:python 获取未来七天的天气情况(1)
- ajax获取当前城市天气情况
- Python使用免费天气API,获取全球任意地区的天气情况
- 利用百度API获取当前用户的省市地区,适合定位精度要求不高的项目
- 代码空间项目 -- 获取当前时间之前的某一天-Calender类的使用
- 实践项目三:桌面版python 获取未来七天的天气情况(2)
- JS获取当前地区的天气信息和当前用户的IP地址
- c#中获取当前年月最后一天
- JS操作日期,获取当前日期,加一天,减一天
- webservice 获取天气情况
- Axis调用webservice查看天气情况(固定地区)
- ABAP如何获取当前月的第一天和最后一天
- JS获取当前网址、主机地址项目根路径
- java获取当前月和周的第一天和最后一天
- mysql获取当前时间,前一天,后一天
- java 获取当前月第一天和最后一天
- JS获取当前月的第一天和最后一天
- 获取所有非manager员工当前的薪水情况
- iOS 10获取当前定位信息,根据位置获取天气