javafx做的天气预报小程序
javafx做的天气预报小程序:
数据来源于中央天象台提供的api 。处理的是Json数据,可以播报6天的天气。
天气的数据来源中央气象台. http://m.weather.com.cn/data/101200101.html 其中 101200101 为武汉的城市代码。通过这个网站获取json数据,然后解析json数据,提取相应的天气信息。
1. 软件只添加了几个城市,大家如果有好的方法能够将全国的城市(精确到县一级)都添加进去,希望提提意见。将全国的城市代码都加到CityList.XML文件中不是个好方法......
2. 另外,通过ip显示当前城市,然后显示当前城市的天气这个时灵时不灵(可能是我这网络的原因,获取ip的网站有时不能访问)。通过访问网站 http://61.4.185.48:81/g/ 得到本机访问的IP,根据此IP查询到城市ID。
效果图:
Json数据解析代码:
public Weather(String Cityid) throws IOException, NullPointerException { // 解析本机ip地址 this.Ctiyid = Cityid; // 连接中央气象台的API URL url = new URL("http://m.weather.com.cn/data/" + Ctiyid + ".html"); connectionData = url.openConnection(); connectionData.setConnectTimeout(1000); try { br = new BufferedReader(new InputStreamReader( connectionData.getInputStream(), "UTF-8")); sb = new StringBuilder(); String line = null; while ((line = br.readLine()) != null) sb.append(line); } catch (SocketTimeoutException e) { System.out.println("亲,网络没有连接"); } catch (FileNotFoundException e) { System.out.println("亲,网络没有连接"); } jsonData = JSONObject.fromObject(sb.toString()); weatherinfo = jsonData.getJSONObject("weatherinfo"); // 设置第一页吗的城市名称 日期 city = weatherinfo.getString("city").toString(); week = weatherinfo.getString("week").toString(); date_y = weatherinfo.getString("date_y").toString(); fchh = weatherinfo.getString("fchh").toString(); // 1到6天的天气 weather1 = weatherinfo.getString("weather1").toString(); weather2 = weatherinfo.getString("weather2").toString(); weather3 = weatherinfo.getString("weather3").toString(); weather4 = weatherinfo.getString("weather4").toString(); weather5 = weatherinfo.getString("weather5").toString(); weather6 = weatherinfo.getString("weather6").toString(); // 1到6天的气温 temp1 = weatherinfo.getString("temp1").toString(); temp2 = weatherinfo.getString("temp2").toString(); temp3 = weatherinfo.getString("temp3").toString(); temp4 = weatherinfo.getString("temp4").toString(); temp5 = weatherinfo.getString("temp5").toString(); temp6 = weatherinfo.getString("temp6").toString(); // 1到6天的风况 wind1 = weatherinfo.getString("wind1").toString(); wind2 = weatherinfo.getString("wind2").toString(); wind3 = weatherinfo.getString("wind3").toString(); wind4 = weatherinfo.getString("wind4").toString(); wind5 = weatherinfo.getString("wind5").toString(); wind6 = weatherinfo.getString("wind6").toString(); // 1到6天的风速 fl1 = weatherinfo.getString("fl1").toString(); fl2 = weatherinfo.getString("fl2").toString(); fl3 = weatherinfo.getString("fl3").toString(); fl4 = weatherinfo.getString("fl4").toString(); fl5 = weatherinfo.getString("fl5").toString(); fl6 = weatherinfo.getString("fl6").toString(); // 各种天气指数 index = weatherinfo.getString("index").toString(); index_uv = weatherinfo.getString("index_uv").toString(); index_tr = weatherinfo.getString("index_tr").toString(); index_co = weatherinfo.getString("index_co").toString(); index_cl = weatherinfo.getString("index_cl").toString(); index_xc = weatherinfo.getString("index_xc").toString(); index_d = weatherinfo.getString("index_d").toString(); // 天气情况的图片 img_title1 = weatherinfo.getString("img_title1").toString(); img_title2 = weatherinfo.getString("img_title2").toString(); img_title3 = weatherinfo.getString("img_title3").toString(); img_title4 = weatherinfo.getString("img_title4").toString(); img_title5 = weatherinfo.getString("img_title5").toString(); img_title6 = weatherinfo.getString("img_title6").toString(); img_title7 = weatherinfo.getString("img_title7").toString(); img_title8 = weatherinfo.getString("img_title8").toString(); img_title9 = weatherinfo.getString("img_title9").toString(); img_title10 = weatherinfo.getString("img_title10").toString(); img_title11 = weatherinfo.getString("img_title11").toString(); img_title12 = weatherinfo.getString("img_title12").toString(); }
所需jar包列表:
commons-beanutils.jar
commons-collections-3.0.jar
commons-logging.jar
commons-lang-2.1.jar
json-lib-1.1-jdk13.jar
ezmorph-1.0.2.jar