如何获取中国天气网天气数据

如何获取中国天气网天气数据,第1张

请求方式:>

接口组成:由固定URL加5个不同的参数组成,完整URL需客户端经过固定方式加密后使用。

数据返回:json

完整URL:>

固定URL:>

使用搜索引擎可以得到很多的天气预报接口,这里以某个api为例

>

参数为city=城市名称

尝试获取南昌的天气预报

>

得到下面xml数据

<xml version="10" encoding="utf-8">

<resp>

  <city>南昌</city>

  <updatetime>16:55</updatetime>

  <wendu>5</wendu>

  <fengli>3级</fengli>

  <shidu>90%</shidu>

  <fengxiang>北风</fengxiang>

  <sunrise_1>06:54</sunrise_1>

  <sunset_1>17:19</sunset_1>

  <sunrise_2/>

  <sunset_2/>

  <environment>

    <aqi>26</aqi>

    <pm25>13</pm25>

    <suggest>各类人群可自由活动</suggest>

    <quality>优</quality>

    <MajorPollutants/>

    <o3>44</o3>

    <co>1</co>

    <pm10>14</pm10>

    <so2>4</so2>

    <no2>27</no2>

    <time>16:00:00</time>

  </environment>

  <yesterday>

    <date_1>4日星期五</date_1>

    <high_1>高温 11℃</high_1>

    <low_1>低温 7℃</low_1>

    <day_1>

      <type_1>阴</type_1>

      <fx_1>无持续风向</fx_1>

      <fl_1>微风</fl_1>

    </day_1>

    <night_1>

      <type_1>小到中雨</type_1>

      <fx_1>北风</fx_1>

      <fl_1>3-4级</fl_1>

    </night_1>

  </yesterday>

  <forecast>

    <weather>

      <date>5日星期六</date>

      <high>高温 7℃</high>

      <low>低温 5℃</low>

      <day>

        <type>中到大雨</type>

        <fengxiang>无持续风向</fengxiang>

        <fengli>微风级</fengli>

      </day>

      <night>

        <type>小到中雨</type>

        <fengxiang>无持续风向</fengxiang>

        <fengli>微风级</fengli>

      </night>

    </weather>

    <weather>

      <date>6日星期天</date>

      <high>高温 10℃</high>

      <low>低温 5℃</low>

      <day>

        <type>阴</type>

        <fengxiang>无持续风向</fengxiang>

        <fengli>微风级</fengli>

      </day>

      <night>

        <type>多云</type>

        <fengxiang>无持续风向</fengxiang>

        <fengli>微风级</fengli>

      </night>

    </weather>

    <weather>

      <date>7日星期一</date>

      <high>高温 13℃</high>

      <low>低温 6℃</low>

      <day>

        <type>多云</type>

        <fengxiang>无持续风向</fengxiang>

        <fengli>微风级</fengli>

      </day>

      <night>

        <type>晴</type>

        <fengxiang>无持续风向</fengxiang>

        <fengli>微风级</fengli>

      </night>

    </weather>

    <weather>

      <date>8日星期二</date>

      <high>高温 13℃</high>

      <low>低温 7℃</low>

      <day>

        <type>晴</type>

        <fengxiang>无持续风向</fengxiang>

        <fengli>微风级</fengli>

      </day>

      <night>

        <type>多云</type>

        <fengxiang>无持续风向</fengxiang>

        <fengli>微风级</fengli>

      </night>

    </weather>

    <weather>

      <date>9日星期三</date>

      <high>高温 13℃</high>

      <low>低温 10℃</low>

      <day>

        <type>小雨</type>

        <fengxiang>无持续风向</fengxiang>

        <fengli>微风级</fengli>

      </day>

      <night>

        <type>小雨</type>

        <fengxiang>无持续风向</fengxiang>

        <fengli>微风级</fengli>

      </night>

    </weather>

  </forecast>

  <zhishus>

    <zhishu>

      <name>晨练指数</name>

      <value>不宜</value>

      <detail>有较强降水,请避免户外晨练,建议在室内做适当锻炼,保持身体健康。</detail>

    </zhishu>

    <zhishu>

      <name>舒适度</name>

      <value>较舒适</value>

      <detail>白天有雨,人们会感到有些凉意,但大部分人完全可以接受。</detail>

    </zhishu>

    <zhishu>

      <name>穿衣指数</name>

      <value>较冷</value>

      <detail>建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。</detail>

    </zhishu>

    <zhishu>

      <name>感冒指数</name>

      <value>极易发</value>

      <detail>将有一次强降温过程,天气寒冷,且空气湿度较大,极易发生感冒,请特别注意增加衣服保暖防寒。</detail>

    </zhishu>

    <zhishu>

      <name>晾晒指数</name>

      <value>不宜</value>

      <detail>有较强降水,不适宜晾晒。若需要晾晒,请在室内准备出充足的空间。</detail>

    </zhishu>

    <zhishu>

      <name>旅游指数</name>

      <value>较不宜</value>

      <detail>天气稍凉,有微风,同时有有较强降水,会给出行产生很多麻烦,建议好还是多选择在室内活动!。</detail>

    </zhishu>

    <zhishu>

      <name>紫外线强度</name>

      <value>最弱</value>

      <detail>属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。</detail>

    </zhishu>

    <zhishu>

      <name>洗车指数</name>

      <value>不宜</value>

      <detail>不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。</detail>

    </zhishu>

    <zhishu>

      <name>运动指数</name>

      <value>较不宜</value>

      <detail>有较强降水,建议您选择在室内进行健身休闲运动。</detail>

    </zhishu>

    <zhishu>

      <name>约会指数</name>

      <value>不适宜</value>

      <detail>较强降水天气会给室外约会增添许多麻烦,尽量不要外出约会,最好在室内促膝谈心。</detail>

    </zhishu>

    <zhishu>

      <name>雨伞指数</name>

      <value>带伞</value>

      <detail>有较强降水,您在外出的时候一定要带雨伞,以免被雨水淋湿。</detail>

    </zhishu>

  </zhishus>

</resp>

得到数据后使用XML解析数据即可,最好是建立一个实体类来存储数据

下面是使用DOM方式解析部分数据代码

DocumentBuilder builder = DocumentBuilderFactorynewInstance()newDocumentBuilder();

//is是网络连接得到的输入流

Document doc = builderparse(is);

if(doc==null) throw new Exception();

Element element = docgetDocumentElement();

NodeList childList = elementgetChildNodes();

for(int i=0; i<childListgetLength(); i++){

Element e = (Element) childListitem(i);

String tag = egetTagName();

String text = egetTextContent();

if("city"equals(tag)){

                //城市

}else if("updatetime"equals(tag)){

//更新时间

}else if("wendu"equals(tag)){

//即时温度

}else if("fengli"equals(tag)){

//风力

}else if("shidu"equals(tag)){

//湿度

}else if("fengxiang"equals(tag)){

//风向

}

}

中国国家气象局天气预报接口总共提供了三个:

>

>

>

最详细的信息来自第三个接口。上面url中的101010100是城市代码,这里是北京的城市代码。只需要改变城市代码,就可以得到所在城市的天气信息。笔者在福州,所以选择的城市代码是福州101230101。

在浏览器上输入url:>{"weatherinfo":{"city":"福州","city_en":"fuzhou","date_y":"2012年5月14 日","date":"","week":"星期 一","fchh":"08","cityid":"101230101","temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃","tempF1":"842℉~734℉","tempF2":"788℉~68℉","tempF3":"752℉~68℉","tempF4":"77℉~68℉","tempF5":"752℉~698℉","tempF6":"77℉~716℉","weather1":" 阵雨转中雨","weather2":"中雨转小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨 转阵雨","weather6":"阵雨转小 雨","img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3","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":"微风","wind2":"微风","wind3":"微 风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风","fl1":"小 于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3 级","index":"热","index_d":"天气较热,建议着短裙、短裤、短套装、T恤等夏季服装。年老体弱者宜着长袖衬衫和单 裤。","index48":"暖","index48_d":"较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长 裤。","index_uv":"弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"适 宜","index_co":"较不舒 适","st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18","index_cl":" 较不宜","index_ls":"不太适宜","index_ag":"不易发"}}[/code]

我们可以解析json数据去得到自己想用的天气信息。

天气信息解释:

{

"weatherinfo":{

<!-- 基本信息 -->

"city":"福州",

"city_en":"fuzhou",

"date_y":"2012年5月14日",

"date":"",

"week":"星期一",

"fchh":"08",

"cityid":"101230101",

<!-- 从今天开始到第六天的每天的天气情况,这里的温度是摄氏温度 -->

"temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃",

<!-- 从今天开始到第六天的每天的天气情况,这里的温度是华氏温度 -->

"tempF1":"842℉~734℉","tempF2":"788℉~68℉","tempF3":"752℉~68℉","tempF4":"77℉~68℉","tempF5":"752℉~698℉","tempF6":"77℉~716℉",

<!-- 天气描述 -->

"weather1":"阵雨转中雨","weather2":"中雨转小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨转阵雨","weather6":"阵雨转小雨",

<!-- 天气描述序号 -->

"img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3",

<!-- 名称 -->

"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":"微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风",

<!-- 风速级别描述 -->

"fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级",

<!-- 今天穿衣指数 -->

"index":"热",

"index_d":"天气较热,建议着短裙、短裤、短套装、T恤等夏季服装。年老体弱者宜着长袖衬衫和单裤。",

<!-- 48小时穿衣指数 -->

"index48":"暖","index48_d":"较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",

<!-- 紫外线及48小时紫外线 -->

"index_uv":"弱","index48_uv":"最弱",

<!-- 洗车 -->

"index_xc":"不宜",

<!-- 旅游 -->

"index_tr":"适宜",、

<!-- 舒适指数 -->

"index_co":"较不舒适",

"st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18",

<!-- 晨练 -->

"index_cl":"较不宜",

<!-- 晾晒 -->

"index_ls":"不太适宜",

<!-- 过敏 -->

"index_ag":"不易发"

}

}

前期的准备工作:

一、申请API(拿好appid和private_key)

二、解读《SmartWeatherAPI<Lite> WebAPI版接口使用说明书》

三、准备好areaid、type、date、appid、urlencode($key)(注意,这里经加密的key是需要encodeurl之后的才能成为接口链接的一部分)

好了下面的编码开始:

1、从附件中的areaid_list中找到你想要的地方的areaid,并且选择要查询天气的类型

NSString areaid = @"101010100";

NSString type =

@"index_f";

/

官方文档更新的数据类型号

指数:index_f(基础接口);index_v(常规接口)

3天预报:forecast_f(基础接口);forecast_v(常规接口)

/

2、获得当前天气date

NSDate

_date = [NSDate date];

NSDateFormatter dateFormatter =

[[NSDateFormatter alloc] init];

[dateFormatter

setDateFormat:@"yyyyMMddHHmmss"];//注意日期的格式

NSString date =

[[dateFormatter stringFromDate:_date]

substringToIndex:12];//用到的精确到分,24小时制60分钟制

3、申请的appid,和private_key

NSString appid =

@"15ds45s13a465s";//这里是楼主随便输入的,瞎编的

NSString private_key =

@"46s4ds_SmartWeatherAPI_45s44d6";//也是瞎编的

4、算出经过urlencode后的key,这步比较重要,步骤也多,请耐心看完。

在原来的的基础上是在PHP的环境中算出的,代码如下,可在“

>

以上就是关于如何获取中国天气网天气数据全部的内容,包括:如何获取中国天气网天气数据、天气预报API接口是什么、android怎样使用天气预报接口 实现天气预报等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9291436.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存