package com.smartdot.dcu;

/**
* java获取新浪天气预报代码
*/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
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 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 CommonsWeatherUtils { 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 CommonsWeatherUtils() { } /**
* 通过输入流来获取新浪接口信息
* @param inStream
*/
public CommonsWeatherUtils(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 CommonsWeatherUtils(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 CommonsWeatherUtils(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 String getWeather(String address){
String weathers = "";
String addressGBK = "";
try {
addressGBK = URLEncoder.encode(address, "GBK");
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
} String link = "http://php.weather.sina.com.cn/xml.php?city="+addressGBK+"&password=DJOYnieT8234jlsK&day=0"; URL url; try {
url = new URL(link);
CommonsWeatherUtils parser = new CommonsWeatherUtils(url);
// 城市 白天天气,白天温度,夜晚天气,夜晚温度,白天风向,白天风力,夜晚风向,夜晚风力,污染指数,污染扩散指数
String[] nodes = {"city","status1","temperature1","status2","temperature2","direction1","power1","direction2","power2","pollution_l","pollution_s"};
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])+"℃ "+map.get(nodes[5])+map.get(nodes[6])+
map.get(nodes[7])+map.get(nodes[8])+map.get(nodes[9])+map.get(nodes[10]));
weathers = 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();
} return weathers;
} public static void main(String[] args) { CommonsWeatherUtils cwu = new CommonsWeatherUtils();
System.out.println(cwu.getWeather("大连")); //String link="http://php.weather.sina.com.cn/xml.php?city=%D6%D8%C7%EC&password=DJOYnieT8234jlsK&day=0";
//String link = "http://php.weather.sina.com.cn/search.php?city=%B4%F3%C1%AC&dpc=1";
// String link = "http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0";
// try {
// System.out.println(URLEncoder.encode("北京", "GBK"));
// } catch (UnsupportedEncodingException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
//
// URL url;
//
// try {
//
// url = new URL(link);
// CommonsWeatherUtils parser = new CommonsWeatherUtils(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();
// } }
}

新浪天气api的更多相关文章

  1. linux - 使用curl实现新浪天气API应用

    新浪天气API的使用方法: API地址:http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT82 ...

  2. 根据新浪天气API获取各地天气状况(Java实现)

    原文出自 参考网址(重要) http://blog.csdn.net/cyxlzzs/article/details/7602469  新浪 http://blog.csdn.net/l_ch_g/a ...

  3. 获取新浪天气api显示天气情况(转)

    直接上一个html的demo <!doctype html> <html class="no-js fixed-layout"> <head> ...

  4. android WebView将新浪天气为我所用 ------>仅供娱乐

    新浪天气提供了一个网页     http://w.sina.com 浏览器访问: 这效果还可以了哦,直接用webview加载出来,效果也可以了哦,不过,这不是我要的.我不希望在我写的应用里到处铺满si ...

  5. 新浪新闻API

    新浪新闻API ustcmio 关注 2017.01.15 20:44* 字数 536 阅读 2479评论 2喜欢 7 新浪新闻的API:1.访问手机新浪网https://sina.cn/?from= ...

  6. scrapy新浪天气

    一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用到桌面上的程序: LX终端(LXTermin ...

  7. [threeJs][新浪股票api][css3]3D新浪财经数据-最近A股涨的也太疯了......

    使用threeJS搭配新浪股票财经API 在线: http://wangxinsheng.herokuapp.com/stock 截图: A股涨幅榜[一片红10%] 检索[单击添加到自选内,自选使用l ...

  8. 新浪通过API分享 实践

    注:如果集成了百度的Frontia和SinaCoreSDK, 那么SSO会出现包冲突 https://github.com/sinaweibosdk/weibo_android_sdk/issues/ ...

  9. 新浪 股票 API

    新浪期货数据接口 [例子]http://hq.sinajs.cn/list=M0豆粕连续 M0 返回值如下:var hq_str_M0="豆粕连续,145958,3170,3190,3145 ...

随机推荐

  1. sessionid固定与session混淆的一些随想

    以前一直觉得sessionid固定和session混淆就是两个一样的东西,后来发现两者还是要分开来的,主要因为利用场景的不同!!! sessionid固定和session混淆还是需要区分开来的一般情况 ...

  2. #WEB安全基础 : HTTP协议 | 0x4 各种协议与HTTP协议的关系(一个报文的旅行)

    报文是怎么旅行的呢? 在网络中有很多引路人,如HTTP协议,IP协议.TCP协议.DNS协议以及ARP协议. 请看下图,演绎一个报文的旅程 这就是一个报文的完整请求过程,请加以理解并记忆 //本系列教 ...

  3. JavaScript知识精简

      JS单线程,同步,一次执行某一段代码,等到前一个程序执行完毕再执行.,阻塞,安全. 多线程,异步,不用等到前一个程序执行完毕就执行. 数据类型 JavaScript 是 弱类型 语言,但并不是没有 ...

  4. byte[] 解析、转码二三事

    1.先从byte 说起, byte 范围为 0~255 的整数,这个区间是在 int 范围中,所以 当byte 转为 int 时,则为小范围转大范围,隐式转换可以直接转换,反过来就是显式转换 需要Co ...

  5. matlab将rgb图转为灰度图的原理代码

    MyYuanLaiPic = imread('e:/image/matlab/Cluo.jpg');%读取RGB格式的图像 MyFirstGrayPic = rgb2gray(MyYuanLaiPic ...

  6. eleemnt-ui修改主题颜色

    饿了吗的element-ui使用的是淡蓝色的主题,有时候我们可以自定义主题,官方的文档给我们提供了如何修改主题,介绍的很详细,自己试验过后,觉得很不错,一方面怕忘记,一方面写一写. 方法一是在线生成一 ...

  7. Json的转换

    package com.utils; import java.io.IOException; import java.util.List; import org.codehaus.jackson.Js ...

  8. Python RabbitMQ消息队列

    python内的队列queue 线程 queue:不同线程交互,不能夸进程 进程 queue:只能用于父进程与子进程,或者同一父进程下的多个子进程,进行交互 注:不同的两个独立进程是不能交互的.   ...

  9. oracle group by placement可能导致错误结果的bug

    Last week I’ve mentioned on Twitter that we ran into wrong result bug. We found workaround quickly b ...

  10. UTF-8格式txt文件读取字节前三位问题

    今天试着读取一份UTF-8格式的txt文件,内容如下 12345 但是每次读取之后转为String类型,输出字符串长度总是为6,并且第一位打印在控制台后不占任何空间. 经过debug查看字节码后发现, ...