`
alvinqq
  • 浏览: 181141 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java类获取天气预报信息

    博客分类:
  • java
阅读更多
网上有很多通过iframe的形式来显示其他网站上的天气预报,这样有一点不好,没有自己的风格,如果通过类来读取这些天气信息,然后我们就可以在页面上构造自己想要的效果和风格。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class GetWeather {

	public String getweather(String city)
	{
		URL ur;
		try {
			ur = new URL("http://www.google.com/ig/api?hl=zh_cn&weather="+city);
			InputStream instr = ur.openStream();
			String s, str;
			BufferedReader in = new BufferedReader(new InputStreamReader(instr));
			StringBuffer sb = new StringBuffer();
			Writer out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("weather.txt"),"utf-8"));
			while ((s = in.readLine()) != null) {
					sb.append(s);
			}
			str = new String(sb);
			out.write(str);
			out.close();
			in.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		File f=new File("weather.txt");
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		String str=null;
		try{
			DocumentBuilder builder=factory.newDocumentBuilder();
			Document doc = builder.parse(f);
			NodeList nl =(NodeList) doc.getElementsByTagName("forecast_conditions");
			NodeList n2=nl.item(0).getChildNodes();
			str=n2.item(4).getAttributes().item(0).getNodeValue()+","+n2.item(1).getAttributes().item(0).getNodeValue()+"℃-"+n2.item(2).getAttributes().item(0).getNodeValue()+"℃";
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return str;
	}
	public static void main(String [] arg)
	{
		//注意weather那写入城市的拼音转化一下就行打开之后是XML格式的然后再提取
		System.out.println(new GetWeather().getweather("zhuzhou"));//查询株洲的天气信息
	}

}


XML文件如下,这文件是从http://www.google.com/ig读取到的
<?xml version="1.0"?>
<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="Zhuzhou, Hunan"/>
		<postal_code data="zhuzhou"/>
		<latitude_e6 data=""/>
		<longitude_e6 data=""/>
		<forecast_date data="2009-10-22"/>
		<current_date_time data="2009-10-22 23:00:00 +0000"/>
		<unit_system data="SI"/>
	</forecast_information>
	<current_conditions>
		<condition data="多云"/>
		<temp_f data="72"/>
		<temp_c data="22"/>
		<humidity data="湿度: 47%"/>
		<icon data="/ig/images/weather/mostly_cloudy.gif"/>
		<wind_condition data="风向: 西、风速:2 米/秒"/>
	</current_conditions>
	<forecast_conditions>
		<day_of_week data="周四"/>
		<low data="16"/>
		<high data="24"/>
		<icon data="/ig/images/weather/mostly_sunny.gif"/>
		<condition data="以晴为主"/>
	</forecast_conditions>
	<forecast_conditions>
		<day_of_week data="周五"/>
		<low data="15"/>
		<high data="27"/>
		<icon data="/ig/images/weather/sunny.gif"/>
		<condition data="晴"/>
	</forecast_conditions>
	<forecast_conditions>
		<day_of_week data="周六"/>
		<low data="16"/>
		<high data="28"/>
		<icon data="/ig/images/weather/sunny.gif"/>
		<condition data="晴"/>
	</forecast_conditions>
	<forecast_conditions>
		<day_of_week data="周日"/>
		<low data="17"/>
		<high data="28"/>
		<icon data="/ig/images/weather/sunny.gif"/>
		<condition data="晴"/>
	</forecast_conditions>
</weather>
</xml_api_reply>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics