Java获取Google的简单天气预报
早上突然看到一个这样的帖子,顺便也自己写了下,现将代码贴出来,获取以后项目可以用到
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
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℃
文件内容(通过工具格式化了,看起来比较方便):
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
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>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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℃
文件内容(通过工具格式化了,看起来比较方便):
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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>