闲赋在家,无事可做就想着做点东西,于是乎把玩手机,我最常用的就是看天气,基本上我每天起来第一件事就是看天气,哈哈,用别人的这么爽,为什么不自己整一个关于天气的应用呢,墨迹天气、小米系统自带的天气、yahoo天气,这几个我比较了下,发现墨迹还是综合起来比较强大,那就从模仿墨迹天机开始吧。

当然我没有服务端的人员,数据只能从网上的开放服务接口来用。

先来看下接口来源和用法吧。

天气的接口和用法

国内的天气预报数据,主要是国家气象局和国外两个来源(指数据来源)。

  • 国外方面主要是雅虎(XML格式,谷歌已经停掉了自家的天气API);
  • 国内数据由国家气象局提供(JSON格式)。

雅虎的那一套不知是不是稳定,再说了支持下国家气象局就是支持人民,我相信人民。

1. 实时天气接口

有多个接口,好像差别不大:
实时天气1:http://www.weather.com.cn/data/sk/101010100.html
实时天气2:http://www.weather.com.cn/data/cityinfo/101010100.html
实时天气3(带时间戳):http://www.weather.com.cn/data/sk/101010100.html?_=1381891661455

2. 一周天气预报接口

    • 详细接口分析如下:

      //格式说明
      var format={fa:图片1,fb:图片2,fc:温度1,fd:温度2,fe:风向1,ff:风向2,fg:风力1,fh:风力2,fi:日出日落};
      //定义天气类型
      var weatherArr={"00":"晴","01":"多云","02":"阴","03":"阵雨","04":"雷阵雨","05":"雷阵雨伴有冰雹","06":"雨夹雪","07":"小雨","08":"中雨","09":"大雨","10":"暴雨","11":"大暴雨","12":"特大暴雨","13":"阵雪","14":"小雪","15":"中雪","16":"大雪","17":"暴雪","18":"雾","19":"冻雨","20":"沙尘暴","21":"小到中雨","22":"中到大雨","23":"大到暴雨","24":"暴雨到大暴雨","25":"大暴雨到特大暴雨","26":"小到中雪","27":"中到大雪","28":"大到暴雪","29":"浮尘","30":"扬沙","31":"强沙尘暴","53":"霾","99":""};
      //定义风向数组
      var fxArr={"0":"无持续风向","1":"东北风","2":"东风","3":"东南风","4":"南风","5":"西南风","6":"西风","7":"西北风","8":"北风","9":"旋转风"};
      //定义风力数组
      var flArr={"0":"微风","1":"3-4级","2":"4-5级","3":"5-6级","4":"6-7级","5":"7-8级","6":"8-9级","7":"9-10级","8":"10-11级","9":"11-12级"};

    • 6天预报数据 URL: http://m.weather.com.cn/data/101190408.html
      该接口来源网络,应该是一个流传比较久的接口,大发现准确性和稳定性可能不稳定。接口分析如下:

      {"weatherinfo":{
      #基本信息
      "city":"北京","city_en":"beijing","date_y":"2013年9月24日","date":"","week":"星期二","fchh":"11","cityid":"101010100",
      #6天温度范围
      "temp1":"22℃~11℃","temp2":"25℃~11℃","temp3":"26℃~13℃","temp4":"25℃~15℃","temp5":"25℃~16℃","temp6":"24℃~16℃",
      #6天温度范围(华氏)
      "tempF1":"71.6℉~51.8℉","tempF2":"77℉~51.8℉","tempF3":"78.8℉~55.4℉","tempF4":"77℉~59℉","tempF5":"77℉~60.8℉","tempF6":"75.2℉~60.8℉",
      #6天天气
      "weather1":"多云转晴","weather2":"晴","weather3":"晴","weather4":"晴转多云","weather5":"阴转阵雨","weather6":"多云",
      #6天天气图示(每天2张)
      "img1":"1","img2":"0","img3":"0","img4":"99","img5":"0","img6":"99","img7":"0","img8":"1","img9":"2","img10":"3","img11":"1","img12":"99",
      "img_single":"1",
      #6天天气图示文字(每天2)
      "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":"多云",
      #6天风
      "wind1":"北风3-4级转微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风",
      "fx1":"北风","fx2":"微风",
      #6天风力情况
      "fl1":"3-4级转小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级",
      #24小时内
      "index":"较冷","index_d":"建议着大衣、呢外套加毛衣、卫衣等服装。体弱者宜着厚外套、厚毛衣。因昼夜温差较大,注意增减衣服。",
      #48小时内
      "index48":"较舒适","index48_d":"建议着薄外套或牛仔衫裤等服装。年老体弱者宜着夹克衫、薄毛衣等。昼夜温差较大,注意适当增减衣服。",
      #紫外 洗车 旅游 舒适指数 晨练 晾晒 过敏
      "index_uv":"弱",
      "index48_uv":"强",
      "index_xc":"适宜",
      "index_tr":"适宜",
      "index_co":"舒",
      "st1":"21","st2":"12","st3":"24","st4":"12","st5":"25","st6":"14", #不明
      "index_cl":"较适宜",
      "index_ls":"适宜",
      "index_ag":"极易发"
      }}

3. 相应图片信息接口

不同天气对应不同图片,每天两张图片对应白天和晚上天气,下面以晴天为例演示几种可用的图片类型。

来自weather.com.cn的图片: 20x20:http://m.weather.com.cn/img/c0.gif
50x46:http://m.weather.com.cn/img/b0.gif

29x20:http://www.weather.com.cn/m/i/weatherpic/29x20/d0.gif #白天带黄色背景

29x20:http://www.weather.com.cn/m2/i/icon_weather/29x20/n00.gif #夜间带深色背景

城市LOGO:http://poster.weather.com.cn/p_files/base/101020100.jpg

c打头的图片是2020像素的,b打头的是5046像素的,d打头的是反白的图标,2920像素,n打头的是夜间反白图标,2920像素,注意这里的文件名是两位数字!

来自mobile.weather.com的图片(图片看起来更美观)):

晴天:http://mobile.weather.com.cn/images/day/00.png

多云:http://mobile.weather.com.cn/images/day/01.png

多云晚上: http://mobile.weather.com.cn/images/night/01.png

下雨:http://mobile.weather.com.cn/images/day/07.png

阴天:http://mobile.weather.com.cn/images/day/02.png

4. 其他数据获取接口

天气资讯数据: http://mobile.weather.com.cn/data/news/khdjson.htm?_=1381891660018

室外活动指数数据:http://mobile.weather.com.cn/data/zsM/101010100.html?_=1381891661502

3H预测数据接口: http://mobile.weather.com.cn/data/forecast3h/101010100.html?_=1381891661605

如有特殊天气发生,通过预警接口可获取预警数据,详细如下:

特殊天气预警接口:http://product.weather.com.cn/alarm/stationalarm.php?areaid=101190401&count=-1&_=1381894097508

信息格式如下:


var alarminfo={"count":"25","data":[["江苏省","**10119-20131204220706-1203.html**"]]};

预警内容获取:http://www.weather.com.cn/data/alarm/10119-20131204220706-1203.html
预警信息页面:http://mobile.weather.com.cn/yj_content.html?file=10119-20131204220706-1203.html

var alarminfo={
"ALERTID":"201312042208582381大雾橙色",
"PROVINCE":"江苏省",
"CITY":"","STATIONNAME":"",
"SIGNALTYPE":"大雾",
"SIGNALLEVEL":"橙色",
"ISSUETIME":"2013-12-04 22:07:06",
"ISSUECONTENT":"江苏省气象台13年12月4日21时57分发布大雾橙色预警信号。目前淮安、盐城、南通、扬州和泰州的大部分地区已经出现能见度小于200米的雾并将持续,另外预计未来12小时内徐州、连云港、宿迁、南京、镇江、常州、无锡和苏州的大部分地区将出现能见度小于1000米、大于等于200米的雾。",
"UNDERWRITER":"魏建苏",
"RELIEVETIME":"2013-12-05 22:07:06"}

5. 获取全国所有城市代码列表

方法一
XML接口根节点: http://flash.weather.com.cn/wmaps/xml/china.xml

XML接口主要作用是递归获取全国几千个县以上单位的城市代码,如:

江苏的XML地址为:http://flash.weather.com.cn/wmaps/xml/shanghai.xml

苏州的XML地址为:http://flash.weather.com.cn/wmaps/xml/jiangsu.xml

上面页面获得太仓city code:101190408

合成太仓天气信息地址:http://m.weather.com.cn/data/101190408.html

下面贴一段PHP代码实现的,通过XML接口根节点递归获得全国几千个县以上城市cide code的代码,供参考(可直接在终端下运行):

方法二

更新:最新发现可以一次性获取全国+国外主要城市,8763个城市列表信息。

URL:http://mobile.weather.com.cn/js/citylist.xml

Yahoo天气预报接口

URL: http://weather.yahooapis.com/forecastrss?w=2151330&u=c

该接口没有测试。



二 环境指数接口

最近大半个中国发生的严重雾霾天气,震惊了我们的老百姓,PM2.5指数概念深入人心。目前有两个接口推荐:

  • pm25.in,需要申请KEY,次数有限制,数据内容丰富,格式清楚;
  • 国家气象局,无需KEY,且次数无限制,数据内容简单,无PM10数据。
pm25.in PM10/PM2.5数值查询接口

pm25.in主页,pm25.in是一家第三方提供pm2.5/pm10环境数据的网站,数据来源于中国国家环境监测中心,其接口使用需要申请KEY(具体请参考其官方网站)。

PM10调用:http://www.pm25.in/api/querys/pm10.json?city=%E4%B8%8A%E6%B5%B7&token=5j1znBVAsnSf5xQyNQyq&avg(该链接中的Key仅供测试用,请自行申请)

数据格式如下:(最后一行为所有检测点平均值

[
{"aqi":147,"area":"上海","pm10":180,"pm10_24h":167,"position_name":"普陀","primary_pollutant":"颗粒物(PM2.5)","quality":"轻度污染","station_code":"1141A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":163,"area":"上海","pm10":151,"pm10_24h":156,"position_name":"十五厂","primary_pollutant":"颗粒物(PM2.5)","quality":"中度污染","station_code":"1142A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":150,"area":"上海","pm10":0,"pm10_24h":0,"position_name":"虹口","primary_pollutant":"颗粒物(PM2.5)","quality":"轻度污染","station_code":"1143A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":170,"area":"上海","pm10":0,"pm10_24h":0,"position_name":"徐汇上师大","primary_pollutant":"颗粒物(PM2.5)","quality":"中度污染","station_code":"1144A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":0,"area":"上海","pm10":0,"pm10_24h":0,"position_name":"杨浦四漂","primary_pollutant":null,"quality":null,"station_code":"1145A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":79,"area":"上海","pm10":115,"pm10_24h":108,"position_name":"青浦淀山湖","primary_pollutant":"颗粒物(PM10)","quality":"良","station_code":"1146A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":160,"area":"上海","pm10":0,"pm10_24h":121,"position_name":"静安监测站","primary_pollutant":"颗粒物(PM2.5)","quality":"中度污染","station_code":"1147A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":150,"area":"上海","pm10":198,"pm10_24h":135,"position_name":"浦东川沙","primary_pollutant":"颗粒物(PM2.5)","quality":"轻度污染","station_code":"1148A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":152,"area":"上海","pm10":152,"pm10_24h":150,"position_name":"浦东新区监测站","primary_pollutant":"颗粒物(PM2.5)","quality":"中度污染","station_code":"1149A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":0,"area":"上海","pm10":0,"pm10_24h":0,"position_name":"浦东张江","primary_pollutant":null,"quality":null,"station_code":"1150A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":146,"area":"上海","pm10":159,"pm10_24h":139,"position_name":null,"primary_pollutant":null,"quality":"轻度污染","station_code":null,"time_point":"2013-12-04T19:00:00Z"}
]

PM2.5调用:http://www.pm25.in/api/querys/pm2_5.json?city=%E4%B8%8A%E6%B5%B7&token=5j1znBVAsnSf5xQyNQyq&avg(该链接中的Key仅供测试用,请自行申请)
数据格式如下:(最后一行为所有检测点平均值

[
{"aqi":147,"area":"上海","pm2_5":105,"pm2_5_24h":112,"position_name":"普陀","primary_pollutant":"颗粒物(PM2.5)","quality":"轻度污染","station_code":"1141A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":163,"area":"上海","pm2_5":129,"pm2_5_24h":124,"position_name":"十五厂","primary_pollutant":"颗粒物(PM2.5)","quality":"中度污染","station_code":"1142A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":150,"area":"上海","pm2_5":124,"pm2_5_24h":115,"position_name":"虹口","primary_pollutant":"颗粒物(PM2.5)","quality":"轻度污染","station_code":"1143A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":170,"area":"上海","pm2_5":141,"pm2_5_24h":129,"position_name":"徐汇上师大","primary_pollutant":"颗粒物(PM2.5)","quality":"中度污染","station_code":"1144A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":0,"area":"上海","pm2_5":0,"pm2_5_24h":0,"position_name":"杨浦四漂","primary_pollutant":null,"quality":null,"station_code":"1145A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":79,"area":"上海","pm2_5":0,"pm2_5_24h":0,"position_name":"青浦淀山湖","primary_pollutant":"颗粒物(PM10)","quality":"良","station_code":"1146A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":160,"area":"上海","pm2_5":103,"pm2_5_24h":122,"position_name":"静安监测站","primary_pollutant":"颗粒物(PM2.5)","quality":"中度污染","station_code":"1147A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":150,"area":"上海","pm2_5":163,"pm2_5_24h":115,"position_name":"浦东川沙","primary_pollutant":"颗粒物(PM2.5)","quality":"轻度污染","station_code":"1148A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":152,"area":"上海","pm2_5":127,"pm2_5_24h":116,"position_name":"浦东新区监测站","primary_pollutant":"颗粒物(PM2.5)","quality":"中度污染","station_code":"1149A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":0,"area":"上海","pm2_5":0,"pm2_5_24h":0,"position_name":"浦东张江","primary_pollutant":null,"quality":null,"station_code":"1150A","time_point":"2013-12-04T19:00:00Z"},
{"aqi":146,"area":"上海","pm2_5":127,"pm2_5_24h":119,"position_name":null,"primary_pollutant":null,"quality":"轻度污染","station_code":null,"time_point":"2013-12-04T19:00:00Z"}
]

注意: 部分城市可能没有pm2.5/pm10数据;

气象局接口PM2.5数值查询接口

气象局接口目前提供PM2.5和AQI数据查询,暂没有发现PM10查询接口。该接口目前不需要申请KEY,可以直接使用。

接口:http://mobile.weather.com.cn/data/air/101190401.html?_=1386217771438
页面:http://mobile.weather.com.cn/air/101190401.html

格式如下:

{"k":{"
k1":"?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?",
"k2":"246|239|156|?|114|114|105|105|119|139|290|300|178|178|193|217|217|217|217|217|217|217|217|217|217",
"k3":"296|289|206|?|148|148|137|137|155|315|340|368|228|228|243|267|267|267|267|267|267|267|267|267|267",
"k4":"201312051100"}}

解析方法如下: ```
$pm2d5Str = result.k.k2;
$pm2d5Arr = result.k.k3.split("|");
$pm2d5 = $pm2d5Arr[23]; $aqiStr=result.k.k3;
$aqiArr=result.k.k3.split("|");
$aqi = $aqiArr[23]; $publish_date=result.k.k4;

参考

[Android]天气App 1的更多相关文章

  1. 制作一个功能丰富的Android天气App

    简易天气是一个基于和风天气数据采用MD设计的Android天气App.目前的版本采用传统的MVC模式构建.通过丰富多彩的页面为用户提供日常所需的天气资讯. 项目说明 项目放在github上面 地址是: ...

  2. [Android]天气App 2 项目搭建

       对于天气App,为了简化一些功能,暂时模仿MUUI系统提供的那个App.    本项目需要引入本人经常使用的一个工具库DroidTool,这个是本人根据工作中,收集到一些工具类,下载地址.    ...

  3. [Android]天气App 3 网络数据的请求和Json解析

      Android客户端开发,不仅仅是在Android端开发,还需要有相应的后台服务支持,否则的话,客户端的数据就只能放到本地自己做处理.我认为的原生态的App就是对应服务端的Client.他能像浏览 ...

  4. [android] 天气app布局练习(四)

    主要练习一下获取网络数据和解析xml MainActivity.java package com.example.weatherreport; import java.io.UnsupportedEn ...

  5. [android] 天气app布局练习(二)

    主要练习一下GridView MainActivity.java package com.example.weatherreport; import java.util.ArrayList; impo ...

  6. [android] 天气app布局练习(三)

    主要练习LinearLayout和layout_weight属性 <RelativeLayout xmlns:android="http://schemas.android.com/a ...

  7. [android] 天气app布局练习

    主要练习一下RelativeLayout和LinearLayout <RelativeLayout xmlns:android="http://schemas.android.com/ ...

  8. Android经典项目开发之天气APP实例分享

    原文:Android经典项目开发之天气APP实例分享 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/mzc186/article/details/5 ...

  9. 利用react native创建一个天气APP

    我们将构建一个实列程序:天气App,(你可以在react native 中创建一个天气应用项目),我们将学习使用并结合可定义模板(stylesheets).盒式布局(flexbox).网络通信.用户输 ...

随机推荐

  1. C++中回调函数(CallBack)的使用

    如果试图直接使用C++的成员函数作为回调函数将发生错误,甚至编译就不能通过. 其错误是普通的C++成员函数都隐含了一个传递函数作为参数,亦即“this”指针,C++通过传递this指针给其成员函数从而 ...

  2. How Tomcat Works(四)

    Servlet容器有两个主要的模块,即连接器(connector)与容器(container),本文接下来创建一个连接器来增强前面文章中的应用程序的功能,以一种更优雅的方式来创建request对象和r ...

  3. Dubbo远程调用服务框架原理与示例

    Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成. 主要核心部件: Remoting: 网络通信框架 ...

  4. 结构类模式(一):适配器(Adapter)

    定义 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 类适配器模式 使用继承的方式实现没有提供的接口从而达到适配到新系统的需求. ...

  5. Toast在关闭应用后还显示的解决办法

    1.我们在用Toast的用法就是:Toast.makeText(Context,CharSequence , Duration).show().但有的时候如果你在一次操作当中多次点击一个view的时候 ...

  6. Centos下忘记mysql的root密码的解决方法

    Centos下忘记mysql的root密码的解决方法 一:(停掉正在运行的mysql) [root@NetDakVPS ~]# service mysql stop 二:使用 “--skip-gran ...

  7. missing artifact com.oracle:ojdbc14:jar:10.2.0.2.0解决办法

    下载jar,导入到maven中 下载:http://download.csdn.net/detail/spring123tt/6991897 cmd中输入: mvn install:install-f ...

  8. 分布式文件系统FastDFS设计原理

    原文地址: http://blog.chinaunix.net/uid-20196318-id-4058561.html FastDFS是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker ...

  9. MySQL中你肯定不知道的int隐情

    MySQL中定义id字段为int类型,但是你知道它内部是什么玩意吗? 1.如果定义int类型,但是不声明长度,系统默认为11个长度(这个大家都知道): 2.如果指定长度小于11,实际上系统还是默认为1 ...

  10. PS-添加前景色

    alt+Delete是填充前景色,即ps左边两个颜色块,前面的那个 ctrl+delete填充背景色