再做JAVA天气预报

     看了两周的马士兵J2SE视频,参与了BOSS系统的需求,接下来可能没有太多的时间学习马士兵的J2SE了。目前已经学习到J2SE5.0第九章线程的基本概念、Sleep、Join、Yield、Priority部分了,接下来再学习就是线程同步部分了。网络、GUI、专题日期处理、正则表达式也需要看。这只能利用周末或者业余的时候看了。今天是11月18日,还是争取在12月15日之前看完这些。

 

    马士兵的JDBC连接数据库部分的视频也需要抽空看,做笔记,敲代码。这个就放在12月15日-12月31日之间完成。总之这系列的东西年底需要全部看完。

 

 

    今天头给了一个任务,天气预报的JAVA实现。大致需求是这样的:

     将中国天气网http://www.weather.com.cn/的近十天的天气数据抓取,可以抓取为XML,保存到本地;

     对XML进行解析,将解析出城市、天气、温度、湿度、风向、风速等字段的信息保存在数据库中;

   

    细化一下,需要用到如下技术:

    爬虫抓取

    解析XML(DOM,SAX,DOM4J等)

    MYSQL数据库的使用(设计)

    编写城市大循环算法,可以查找所有城市

    增加根据IP查询当前使用者城市的功能

    定时器,每天自动更新数据库信息  

 

    上次根据网上的代码抄抄改改,已经完成了爬虫抓取、解析XML、MYSQL数据字段的读进。但是毕竟算改写别人的代码,学习了两周J2SE之后,这次要完全写出自己的程序。

    给自己写个时间规划吧:

    现在是2011年11月18日,周五,因为晚上6点要回去早点休息,周六周日阿狸要来北京玩两天,明早5点半得起床去北京站接站。所以离下班只有两小时,今天下班前研究一下网页文件的抓取,抓取保存到本地或者直接在控制台窗口输出。有空的话再研究一下DOM解析和SAX解析。

 

 

    今天是2011年11月21日,找到了中国天气网的接口:

中国天气网接口:

6天预报:http://m.weather.com.cn/data/101010100.html

实时预报:http://www.weather.com.cn/data/sk/101010100.html

其中101010100为查询编码具体查询的时候替换此编码就行了(下面附编码),编码分3部分:

第一部分:10101为省级代码

第二部分:01为市级代码

第三部分:00为市级以下代码;

    

    以上接口直接将我所需要的数据解析为JSON格式了,也就是说我拿到数据之后就可以稍微装模作样的解析一下就塞入数据库中了。

    经过1小时的简单整理,将所有的城市号罗列出来,放在CSDN资源区了。位置如下:

 http://download.csdn.net/detail/opzoonzhuzhengke/3820632

 

    接下来要做的工作就是数据库设计两张表,一张历史表、一张实时表,将内容存入数据库。此外,还需要编写一个定时器的功能,实现定时抓取。

 

    下午写了一段代码,抓取一个网页上的JSON数据,并且显示在控制台上,增加了乱码处理(UTF-8):

    

package com.zzk.cn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @version 1.0.0
 * @author zhuzhengke
 *
 */
public class GetWeather {
	public static void main(String[] args) {
		GetInfo();

	}

	/**
	 * 获取网页信息
	 */
    public static void GetInfo() {
		URL url = null;
		URLConnection conn = null;
		InputStream in = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			url = new URL("http://m.weather.com.cn/data/101010100.html");
			conn = url.openConnection();
			in = conn.getInputStream();
			isr = new InputStreamReader(in, "UTF-8");
			br = new BufferedReader(isr);

			String line = "";
			while (null != (line = br.readLine())) {
				System.out.println(line);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != br) {
					br.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			br = null;

			try {
				if (null != isr) {
					isr.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			isr = null;

			try {
				if (null != in) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			in = null;
		}
    }

}

2011年11月22日

今天在ECLIPSE项目下增加了一个weather_code.properties,抓取和JSON解析都实现了,贴下代码,明天要设计数据库,并且把属性信息放到WEATHER包装类中:

package com.zzk.cn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

import net.sf.json.JSONObject;

/**
 *
 * @author zhuzhengke
 * @version 1.0.0
 *
 */

class ReadProperties {  

    public String getProperties(String key) {
        Properties prop = new Properties();
        InputStream inputStream = this.getClass().getResourceAsStream("/weather_code.properties");
        try {
            prop.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }  finally {
        	//关闭
            if (null != inputStream) {
                try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
            }
        }

        return prop.getProperty(key);
    }  

} 

public class GetWeather {

	static String info = "";

	/*主函数*/
	public static void main(String[] args) throws UnsupportedEncodingException {
		GetInfo();
		Json();
		Properties prop = new Properties();
        InputStream inputStream = new GetWeather().getClass().getResourceAsStream("/weather_code.properties");
        try {
			prop.load(inputStream);
			System.out.println(prop.getProperty("101020100"));
			Enumeration<?> en = prop.propertyNames();
			while(en.hasMoreElements()) {
				String obj = (String)en.nextElement();
				System.out.println(obj);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//关闭资源
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * Json解析
	 */
	public static void Json() {

		//以下为JSON格式抓取实例
		/**
		 * {"weatherinfo":{"city":"北京","city_en":"beijing","date_y":
		 * "2011年11月22日","date":"辛卯年","week":"星期二","fchh":"11",
		 * "cityid":"101010100"
		 * ,"temp1":"12℃~-2℃","temp2":"7℃~-3℃","temp3":"8℃~-1℃"
		 * ,"temp4":"12℃~3℃","temp5":"12℃~4℃",
		 * "temp6":"11℃~4℃","tempF1":"53.6℉~28.4℉"
		 * ,"tempF2":"44.6℉~26.6℉","tempF3"
		 * :"46.4℉~30.2℉","tempF4":"53.6℉~37.4℉",
		 * "tempF5":"53.6℉~39.2℉","tempF6"
		 * :"51.8℉~39.2℉","weather1":"多云转晴","weather2"
		 * :"晴","weather3":"晴","weather4":"晴转多云",
		 * "weather5":"多云转晴","weather6":"阴"
		 * ,"img1":"1","img2":"0","img3":"0","img4"
		 * :"99","img5":"0","img6":"99","img7":"0","img8":"1",
		 * "img9":"1","img10"
		 * :"0","img11":"2","img12":"99","img_single":"1","img_title1"
		 * :"多云","img_title2":"晴","img_title3":"晴",
		 * "img_title4":"晴","img_title5"
		 * :"晴","img_title6":"晴","img_title7":"晴","img_title8"
		 * :"多云","img_title9":"多云","img_title10":"晴",
		 * "img_title11":"阴","img_title12"
		 * :"阴","img_title_single":"多云","wind1":"北风4-5级"
		 * ,"wind2":"微风","wind3":"微风","wind4":"微风","wind5":
		 * "微风","wind6":"微风","fx1"
		 * :"北风","fx2":"北风","fl1":"4-5级","fl2":"小于3级","fl3"
		 * :"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级",
		 * "index":"凉","index_d":
		 * "天气凉,建议着厚外套加毛衣等春秋服装。体弱者宜着大衣、呢外套。因昼夜温差较大,注意增减衣服。"
		 * ,"index48":"冷","index48_d":
		 * "天气冷,建议着棉衣、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣或冬大衣。","index_uv"
		 * :"弱","index48_uv":"中等","index_xc":"较适宜","index_tr":"很适宜",
		 * "index_co":"较舒适"
		 * ,"st1":"11","st2":"-4","st3":"4","st4":"0","st5":"8","st6"
		 * :"0","index_cl":"较不宜","index_ls":"基本适宜","index_ag": "极不易发"}}
		 */
		JSONObject jsonob = JSONObject.fromObject((JSONObject.fromObject(info)
				.getString("weatherinfo")));

		String city = jsonob.getString("city");// 城市
		System.out.println(city);

		String date_y = jsonob.getString("date_y");// 时间
		System.out.println(date_y);

		String date = jsonob.getString("date");// 农历年
		System.out.println(date);

		String week = jsonob.getString("week");// 星期
		System.out.println(week);

		String cityid = jsonob.getString("cityid");// 城市号
		System.out.println(cityid);

		String temp1 = jsonob.getString("temp1");// 以下是六天内摄氏温度
		System.out.println(temp1);

		String temp2 = jsonob.getString("temp2");
		System.out.println(temp2);

		String temp3 = jsonob.getString("temp3");
		System.out.println(temp3);

		String temp4 = jsonob.getString("temp4");
		System.out.println(temp4);

		String temp5 = jsonob.getString("temp5");
		System.out.println(temp5);

		String temp6 = jsonob.getString("temp6");
		System.out.println(temp6);

		String tempF1 = jsonob.getString("tempF1");// 以下是六天内华氏温度
		System.out.println(tempF1);

		String tempF2 = jsonob.getString("tempF2");
		System.out.println(tempF2);

		String tempF3 = jsonob.getString("tempF3");
		System.out.println(tempF3);

		String tempF4 = jsonob.getString("tempF4");
		System.out.println(tempF4);

		String tempF5 = jsonob.getString("tempF5");
		System.out.println(tempF5);

		String tempF6 = jsonob.getString("tempF6");
		System.out.println(tempF6);

		String weather1 = jsonob.getString("weather1");// 以下是六天天气
		System.out.println(weather1);

		String weather2 = jsonob.getString("weather2");
		System.out.println(weather2);

		String weather3 = jsonob.getString("weather3");
		System.out.println(weather3);

		String weather4 = jsonob.getString("weather4");
		System.out.println(weather4);

		String weather5 = jsonob.getString("weather5");
		System.out.println(weather5);

		String weather6 = jsonob.getString("weather6");
		System.out.println(weather6);

		String wind1 = jsonob.getString("wind1");// 以下六天为风力
		System.out.println(wind1);

		String wind2 = jsonob.getString("wind2");
		System.out.println(wind2);

		String wind3 = jsonob.getString("wind3");
		System.out.println(wind3);

		String wind4 = jsonob.getString("wind4");
		System.out.println(wind4);

		String wind5 = jsonob.getString("wind5");
		System.out.println(wind5);

		String wind6 = jsonob.getString("wind6");
		System.out.println(wind6);

		String fl1 = jsonob.getString("fl1");// 以下为六天风级
		System.out.println(fl1);

		String fl2 = jsonob.getString("fl2");
		System.out.println(fl2);

		String fl3 = jsonob.getString("fl3");
		System.out.println(fl3);

		String fl4 = jsonob.getString("fl4");
		System.out.println(fl4);

		String fl5 = jsonob.getString("fl5");
		System.out.println(fl5);

		String fl6 = jsonob.getString("fl6");
		System.out.println(fl6);

		String index_d = jsonob.getString("index_d");// 当日穿衣指数
		System.out.println(index_d);

		System.out.println();

		// System.out.println("以下仅仅是一个Demo");
		// 解析的数据格式2:{"classroom":"0801","peoples":[{"field1":"name1","field2":"age1"},{"field0":"name2","field2":"age2说"}]}
		// info =
		// "{\"classroom\":\"111\",\"peoples\":[{\"field1\":\"zzk1\",\"field2\":\"age1\"},{\"field1\":\"zzk2\",\"field2\":\"age2\"}]}";
		//
		// jsonob = JSONObject.fromObject(info);
		//
		// String classname = jsonob.getString("classroom");
		// System.out.println(classname);
		//
		// JSONArray jsons = jsonob.getJSONArray("peoples");
		// int jsonLength = jsons.size();
		//
		// // 对json数组进行循环
		// for (int i = 0; i < jsonLength; i++) {
		// JSONObject tempJson = JSONObject.fromObject(jsons.get(i));
		//
		// String name = StringEscapeUtils.escapeSql(tempJson
		// .getString("field1"));
		// String age = StringEscapeUtils.escapeSql(tempJson
		// .getString("field2"));
		//
		// System.out.println(name + "-" + age);
		// }
	}

	/**
	 * 获取网页信息
	 */
	public static void GetInfo() {

		String id = "101020100";
		String path = "http://m.weather.com.cn/data/"+id+".html";
		URL url;
		String inputline = "";

		try {
			url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(10 * 1000);
			conn.setRequestMethod("GET");

			InputStreamReader inStream = new InputStreamReader(
					conn.getInputStream(), "UTF-8");
			BufferedReader buffer = new BufferedReader(inStream);

			while ((inputline = buffer.readLine()) != null) {
				info += inputline;
			}

		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//关闭资源
		}
	}

	/**
	 * 封装weather类
	 */
	public class Weather {
		private String city;// 城市名
		private String date;// 日期:yyyy年MM月d日
		private String lunarDate;// 农历日期/当日有
		private String week;// 星期
		private String fcTime;// 预报时间:24制小时数/当日有
		private String temperature;// 当日气温
		private String weather;// 天气
		private String wind;// 风力

		/**
		 *
		 * get和set方法
		 */
		public String getCity() {
			return city;
		}

		public void setCity(String city) {
			this.city = city;
		}

		public String getDate() {
			return date;
		}

		public void setDate(String date) {
			this.date = date;
		}

		public String getLunarDate() {
			return lunarDate;
		}

		public void setLunarDate(String lunarDate) {
			this.lunarDate = lunarDate;
		}

		public String getWeek() {
			return week;
		}

		public void setWeek(String week) {
			this.week = week;
		}

		public String getFcTime() {
			return fcTime;
		}

		public void setFcTime(String fcTime) {
			this.fcTime = fcTime;
		}

		public String getTemperature() {
			return temperature;
		}

		public void setTemperature(String temperature) {
			this.temperature = temperature;
		}

		public String getWeather() {
			return weather;
		}

		public void setWeather(String weather) {
			this.weather = weather;
		}

		public String getWind() {
			return wind;
		}

		public void setWind(String wind) {
			this.wind = wind;
		}

		// 重写toString方法
		@Override
		public String toString() {
			return "Weather [city=" + city + ", date=" + date + ", lunarDate="
					+ lunarDate + ", week=" + week + ", fcTime=" + fcTime
					+ ", temperature=" + temperature + ", weather=" + weather
					+ ", wind=" + wind + "]";
		}

	}

}

 

时间: 2024-08-30 10:48:35

再做JAVA天气预报的相关文章

为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?

为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?  今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术框架由struts2迁移到springMVC,我突然有了一个新的疑问无法得到正确的解释,为什么我们现在做java的web开发,会选择struts2或者springMVC这样的框架,而不是使用servlet加jsp这样的技术呢?特别是现在我们web的前端页面都是使用

linux安装哪个版本好?做java开发,redhat还是其他的

问题描述 linux有好多版本,到底是安装哪个好呢,要做java开发,征求一下大家的意见,我现在有redhat的iso文件,是9.0的,后来的好像就收费了,其他的怎么样呢,大家推荐几款.还有就是我的iso文件有三个,是不是意味着要刻盘的话需要刻三张盘呢?求解. 解决方案 我说一下我的体会:如果开发的话,Fedora是最合适的开发平台,Fedora是红帽的开源版本,每个版本都包含很多新技术,但是可能是我的电脑配置(cpu 1.7GHZ 内存 1G)差点,运行起来有点慢:再一个就是Ubuntu,适合

寻找北京地铁13号线上做java开发的女孩。

问题描述 在2009年1月9日,22:30左右,北京地铁13号线,从西直门至五道口的地铁里遇到一女孩,是做java开发的,她是黑龙江人,好像还在找工作,当时说是在做java开发,但真正找到工作后不一定是做java开发,我本人是做VC开发的,和她交流一些技术方面的问题了,由于种种原因,没能留下联系方式,现在在CSDN技术论坛发贴寻找她,我想不难找到她,在北京做java开发也不过万十左右人,那么女孩做java开发会更少,所以估计最多也就千左右吧,希望论坛管理员不要删除我的贴子,同时希望见此贴的同仁们

做Java项目过程中遇到乱码问题的解决方案

在做java项目(特别是web项目)的过程中,中文乱码一直是我们开发人员比较头疼的问题,因为涉及到编码,解码,字符集,以及国际化等诸多问题,所以在着手解决的时候也缺乏相关的知识.我花了一些时间自己动手实验了一把,虽然没有洞悉编码,解码这些底层原理,但是解决实际问题应该足够了.这里主要针对java web项目中的文乱码问题. 从浏览器采用form方式提交数据到服务器,可以分为post和get方法. 1,post方法: 在jsp页面中的page指令中,有一个pageEncoding,这个指令表示js

给我3亿美元我能再做一个新浪-空中网杨宁

新浪 由俭入奢易,由奢入俭难.1999年,杨宁作为第一批高举互联网大旗回国创业的"海龟"之一,与周云帆.陈一舟共同创办了ChinaRen网站.但 花钱花到手软的美景在一夜间化为一地泡沫,2000年9月14日,第一轮互联网泡沫破灭,杨宁和他的创业伙伴面对无法发放员工工资的窘境,不得不选择并入 搜狐.所谓置之死地而后生,2002年3月18日,杨宁再次创业,与周云帆创办了"空中网",东山再起.2004年7月9日,空中网在美国纳斯达克挂牌上 市,是中国企业从成立到纳斯达克上

再做新站感悟:友情链接的那点事

本来我一直当自己是老站长了,但最近做了个钓鱼方面的新站:钓鱼啦,所以又仿佛做了回新站长,把友做情链接这样恼人的事又给体验了一遍,有感而发在这里谈谈.新站友情链接的话题恐怕不少,我在这里结合具体的经历来说吧. 在友情链接这件事上我是吃过亏的.我一直经营一个在业内挺有名气的行业站(这个就不用AD了,呵呵),做这个站的最初我风风火火拉了很多友情链接,不分行业,只要对方网站热闹就有链接的冲动,那时做友情链接相当幼稚,只为了从友情链接拉访问量,可是一看计数器,从友情链接过来的人寥寥无几,几乎可以忽略,于是

再再谈java乱码:GBK和UTF-8互转尾部乱码问题分析(续)

GBK字节码用UTF-8解码 UTF-8 的编码规则 转码实例 解决问题 jdk 18 测试 jdk 1617 jdk 版本的影响 小结 参考 在<再谈java乱码:GBK和UTF-8互转尾部乱码问题分析>我们分析了,如果从一个UTF-8 的字节序列,经过 new String(b,"GBK") 的操作,"可能"(与总字节数有关)会破坏数据.结果可能是,损失最后一个"字". 反过来呢?可能会很惨,大范围溃散... 同时,可参考:一段j

java mysql-用mysql做java项目开发

问题描述 用mysql做java项目开发 做数据库的时候出了问题如下CSDN移动问答 求好心的大神解答 万分感谢

利用android客户端支付宝sdk的jar包和demo的几个处理支付的类再做一个jar包

问题描述 利用android客户端支付宝sdk的jar包和demo的几个处理支付的类再做一个jar包 利用android客户端支付宝sdk的jar包和demo的几个处理支付的类(不包含activity)再做一个jar包,其它项目中只要导入该包和传入几个参数就可以使用支付宝支付功能 解决方案 将项目导出作为一个jar供他人引用不就好了,或者使用webservice