Java获取Google的简单天气预报

早上突然看到一个这样的帖子,顺便也自己写了下,现将代码贴出来,获取以后项目可以用到

1 package com.potevio.telecom;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.File;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.io.OutputStreamWriter;
11 import java.io.Writer;
12 import java.net.URL;
13 import java.text.DateFormat;
14 import java.util.Date;
15
16 import javax.xml.parsers.DocumentBuilder;
17 import javax.xml.parsers.DocumentBuilderFactory;
18
19 import org.w3c.dom.Document;
20 import org.w3c.dom.NodeList;
21
22 /**
23 * @description 通过Java获取Google的简单天气预报
24 * @author ZhouJingxian
25 *
26 */
27 public class GetGoogleWeather {
28
29 /**
30 *
31 * @param cityName 注意weather那写入城市的拼音转化一下就行,
32 * 打开之后是XML格式的然后再提取。
33 * @return
34 */
35 public String getWeather(String cityName,String fileAddr){
36 //获取google上的天气情况,写入文件
37 try{
38 URL url = new URL("http://www.google.com/ig/api?hl=zh_cn&weather="+cityName);
39 InputStream inputstream = url.openStream();
40 String s,str;
41 BufferedReader in = new BufferedReader(new InputStreamReader(inputstream));
42 StringBuffer stringbuffer = new StringBuffer();
43 Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileAddr),"utf-8"));
44 while((s = in.readLine())!=null){
45 stringbuffer.append(s);
46 }
47 str = new String(stringbuffer);
48 out.write(str);
49 out.close();
50 in.close();
51 }catch(IOException e){
52 e.printStackTrace();
53 }
54
55 //读取需要的数据
56 File file = new File(fileAddr);
57 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
58 String str=null;
59 try{
60 DocumentBuilder builder = factory.newDocumentBuilder();
61 Document doc = builder.parse(file);
62 NodeList nodelist1 = (NodeList)doc.getElementsByTagName("forecast_conditions");
63 NodeList nodelist2 = nodelist1.item(0).getChildNodes();
64
65 str = nodelist2.item(4).getAttributes().item(0).getNodeValue()+",temperature:"
66 +nodelist2.item(1).getAttributes().item(0).getNodeValue()+"℃-"
67 +nodelist2.item(2).getAttributes().item(0).getNodeValue()+"℃";
68 }catch(Exception e){
69 e.printStackTrace();
70 }
71 return str;
72 }
73
74 public static void main(String args[]){
75 GetGoogleWeather ggw = new GetGoogleWeather();
76
77 String cityName = "beijing";
78 String fileAddr = "F:/"+cityName+".xml";
79 String temperature = ggw.getWeather(cityName,fileAddr);
80
81 Date nowDate = new Date();
82 DateFormat dateformat = DateFormat.getDateInstance();
83 String today = dateformat.format(nowDate);
84
85 System.out.println(today+" "+cityName+"的天气情况是:"+temperature);
86 }
87 }
88


控制输出结果:
2009-7-31 beijing的天气情况是:可能有雨,temperature:16℃-31℃
文件内容(通过工具格式化了,看起来比较方便):

1 <?xml version="1.0"?>
2 <xml_api_reply version="1">
3 <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1"
4 row="0" section="0">
5 <forecast_information>
6 <city data="beijing" />
7 <postal_code data="beijing" />
8 <latitude_e6 data="" />
9 <longitude_e6 data="" />
10 <forecast_date data="2009-07-31" />
11 <current_date_time data="2009-07-31 16:32:12 +0000" />
12 <unit_system data="SI" />
13 </forecast_information>
14 <current_conditions>
15 <condition data="晴" />
16 <temp_f data="79" />
17 <temp_c data="26" />
18 <humidity data="湿度: 65%" />
19 <icon data="/ig/images/weather/sunny.gif" />
20 <wind_condition data="风向: 东、风速:2 米/秒" />
21 </current_conditions>
22 <forecast_conditions>
23 <day_of_week data="周五" />
24 <low data="16" />
25 <high data="31" />
26 <icon data="/ig/images/weather/chance_of_rain.gif" />
27 <condition data="可能有雨" />
28 </forecast_conditions>
29 <forecast_conditions>
30 <day_of_week data="周六" />
31 <low data="22" />
32 <high data="31" />
33 <icon data="/ig/images/weather/chance_of_storm.gif" />
34 <condition data="可能有暴风雨" />
35 </forecast_conditions>
36 <forecast_conditions>
37 <day_of_week data="周日" />
38 <low data="19" />
39 <high data="29" />
40 <icon data="/ig/images/weather/chance_of_storm.gif" />
41 <condition data="可能有暴风雨" />
42 </forecast_conditions>
43 <forecast_conditions>
44 <day_of_week data="周一" />
45 <low data="23" />
46 <high data="32" />
47 <icon data="/ig/images/weather/chance_of_rain.gif" />
48 <condition data="可能有雨" />
49 </forecast_conditions>
50 </weather>
51 </xml_api_reply>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值