package com.skywares.dcom.commons;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
/**
* 天气查询. 推荐使用此 google service 查询<br/>
*
*/
public class WeatherUtils
{
/**
* 使用 google 查询天气<br/>
* 上海: http://www.google.com/ig/api?hl=zh_cn&weather=shanghai
*
* @param city
* 城市拼音, 如 北京: beijing
* @return wuhan : 2010-02-27 天气: 晴, 温度: 8 - 20℃, 湿度: 88%, 风向: 东南、风速:4 米/秒
*/
public static String getweather(String city)
{
StringBuilder sbd = new StringBuilder();
try
{
String ur = "http://www.google.com/ig/api?hl=zh_cn&weather=";
URL url = new URL(ur + city);
InputStream in = url.openStream();
// 解决乱码问题
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int i = -1;
while ((i = in.read()) != -1)
bos.write(i);
// 使用 utf-8 编码. 若不使用则默认会使用本地编码 GB18030, 则会有乱码
InputStream inp = new ByteArrayInputStream(bos.toString().getBytes("utf-8"));
// 读取流
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inp);
// 城市:
sbd.append(city).append(" : ");
NodeList n1 = getNode(doc, "forecast_information", 0);
// 日期
sbd.append(getAttributeValue(n1, 4, 0) + " ");
NodeList n2 = getNode(doc, "current_conditions", 0);
System.out.println("天气: " + getAttributeValue(n2, 0, 0));
System.out.println(getAttributeValue(n2, 3, 0));
System.out.println(getAttributeValue(n2, 5, 0));
// 天气
sbd.append("天气: " + getAttributeValue(n2, 0, 0) + ", ");
NodeList n3 = getNode(doc, "forecast_conditions", 0);
// 最低气温
sbd.append("温度: " + getAttributeValue(n3, 1, 0));
sbd.append(" - ");
// 最高气温
sbd.append(getAttributeValue(n3, 2, 0) + "℃, ");
// 湿度
sbd.append(getAttributeValue(n2, 3, 0) + ", ");
// 风向
sbd.append(getAttributeValue(n2, 5, 0));
}
catch (Exception e)
{
sbd.append("获取天气失败或不存在此城市");
}
return sbd.toString();
}
/**
* 获取节点集合
*
* @param doc
* : Doument 对象
* @param tagName
* : 节点名
* @param index
* : 找到的第几个
* @return
*/
private static NodeList getNode(Document doc, String tagName, int index)
{
return doc.getElementsByTagName(tagName).item(index).getChildNodes();
}
/**
* 获取节点内容
*
* @param node
* : nodelist
* @param index
* : 节点索引, 也可使用 getNamedItem(String name) 节点名查找
* @param item
* : 属性的索引
* @return
*/
private static String getAttributeValue(NodeList node, int index, int item)
{
return node.item(index).getAttributes().item(item).getNodeValue();
}
public static void main(String[] args)
{
System.out.println(getweather("wuhan"));
}
}
/*
* 这里是返回的xml
*/
xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
<forecast_information>
<city data="Wuhan, Hubei"/>
<postal_code data="wuhan"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2012-06-28"/>
<current_date_time data="2012-06-28 19:00:00 +0000"/>
<unit_system data="SI"/>
</forecast_information>
<current_conditions>
<condition data="小雨"/>
<temp_f data="79"/>
<temp_c data="26"/>
<humidity data="湿度: 89%"/>
<icon data="/ig/images/weather/cn_lightrain.gif"/>
<wind_condition data="风向: 东南、风速:3 米/秒"/>
</current_conditions>
<forecast_conditions>
<day_of_week data="周四"/>
<low data="24"/>
<high data="29"/>
<icon data="/ig/images/weather/cn_heavyrain.gif"/>
<condition data="雨"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周五"/>
<low data="25"/>
<high data="33"/>
<icon data="/ig/images/weather/thunderstorm.gif"/>
<condition data="雷阵雨"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周六"/>
<low data="23"/>
<high data="33"/>
<icon data="/ig/images/weather/chance_of_storm.gif"/>
<condition data="可能有暴风雨"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周日"/>
<low data="24"/>
<high data="31"/>
<icon data="/ig/images/weather/chance_of_storm.gif"/>
<condition data="可能有暴风雨"/>
</forecast_conditions>
</weather>
</xml_api_reply>
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
/**
* 天气查询. 推荐使用此 google service 查询<br/>
*
*/
public class WeatherUtils
{
/**
* 使用 google 查询天气<br/>
* 上海: http://www.google.com/ig/api?hl=zh_cn&weather=shanghai
*
* @param city
* 城市拼音, 如 北京: beijing
* @return wuhan : 2010-02-27 天气: 晴, 温度: 8 - 20℃, 湿度: 88%, 风向: 东南、风速:4 米/秒
*/
public static String getweather(String city)
{
StringBuilder sbd = new StringBuilder();
try
{
String ur = "http://www.google.com/ig/api?hl=zh_cn&weather=";
URL url = new URL(ur + city);
InputStream in = url.openStream();
// 解决乱码问题
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int i = -1;
while ((i = in.read()) != -1)
bos.write(i);
// 使用 utf-8 编码. 若不使用则默认会使用本地编码 GB18030, 则会有乱码
InputStream inp = new ByteArrayInputStream(bos.toString().getBytes("utf-8"));
// 读取流
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inp);
// 城市:
sbd.append(city).append(" : ");
NodeList n1 = getNode(doc, "forecast_information", 0);
// 日期
sbd.append(getAttributeValue(n1, 4, 0) + " ");
NodeList n2 = getNode(doc, "current_conditions", 0);
System.out.println("天气: " + getAttributeValue(n2, 0, 0));
System.out.println(getAttributeValue(n2, 3, 0));
System.out.println(getAttributeValue(n2, 5, 0));
// 天气
sbd.append("天气: " + getAttributeValue(n2, 0, 0) + ", ");
NodeList n3 = getNode(doc, "forecast_conditions", 0);
// 最低气温
sbd.append("温度: " + getAttributeValue(n3, 1, 0));
sbd.append(" - ");
// 最高气温
sbd.append(getAttributeValue(n3, 2, 0) + "℃, ");
// 湿度
sbd.append(getAttributeValue(n2, 3, 0) + ", ");
// 风向
sbd.append(getAttributeValue(n2, 5, 0));
}
catch (Exception e)
{
sbd.append("获取天气失败或不存在此城市");
}
return sbd.toString();
}
/**
* 获取节点集合
*
* @param doc
* : Doument 对象
* @param tagName
* : 节点名
* @param index
* : 找到的第几个
* @return
*/
private static NodeList getNode(Document doc, String tagName, int index)
{
return doc.getElementsByTagName(tagName).item(index).getChildNodes();
}
/**
* 获取节点内容
*
* @param node
* : nodelist
* @param index
* : 节点索引, 也可使用 getNamedItem(String name) 节点名查找
* @param item
* : 属性的索引
* @return
*/
private static String getAttributeValue(NodeList node, int index, int item)
{
return node.item(index).getAttributes().item(item).getNodeValue();
}
public static void main(String[] args)
{
System.out.println(getweather("wuhan"));
}
}
/*
* 这里是返回的xml
*/
xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
<forecast_information>
<city data="Wuhan, Hubei"/>
<postal_code data="wuhan"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2012-06-28"/>
<current_date_time data="2012-06-28 19:00:00 +0000"/>
<unit_system data="SI"/>
</forecast_information>
<current_conditions>
<condition data="小雨"/>
<temp_f data="79"/>
<temp_c data="26"/>
<humidity data="湿度: 89%"/>
<icon data="/ig/images/weather/cn_lightrain.gif"/>
<wind_condition data="风向: 东南、风速:3 米/秒"/>
</current_conditions>
<forecast_conditions>
<day_of_week data="周四"/>
<low data="24"/>
<high data="29"/>
<icon data="/ig/images/weather/cn_heavyrain.gif"/>
<condition data="雨"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周五"/>
<low data="25"/>
<high data="33"/>
<icon data="/ig/images/weather/thunderstorm.gif"/>
<condition data="雷阵雨"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周六"/>
<low data="23"/>
<high data="33"/>
<icon data="/ig/images/weather/chance_of_storm.gif"/>
<condition data="可能有暴风雨"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周日"/>
<low data="24"/>
<high data="31"/>
<icon data="/ig/images/weather/chance_of_storm.gif"/>
<condition data="可能有暴风雨"/>
</forecast_conditions>
</weather>
</xml_api_reply>