前期的准备工作:
一、申请API(拿好appid和private_key)
二、解读《SmartWeatherAPILite 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的环境中算出的,代码如下,可在“
”下进行算法的检验
echo
urlencode(base64_encode(hash_hmac('sha1', " ;type=index_fdate=201409041509appid=15ds45s13a465s",
"46s4ds_SmartWeatherAPI_45s44d6",
TRUE)));
首先定义得到public_key和API的 *** ,还有就是对key进行encodeurl操作的 ***
注意,这里的 *** 都是被我定义在getTime的类里面,后面是在main中实例化出来的
//获得publicky
- (NSString*)
getPublicKey:(NSString*)areaid :(NSString*)type :(NSString*)date
:(NSString*)appid {
NSString *Key = [[NSString alloc]
initWithFormat:@";type=%@date=%@appid=%@",
areaid, type, [date substringToIndex:12], appid];
return
Key;
}
//获得完整的API
- (NSString*) getAPI:(NSString*)areaid
:(NSString*)type :(NSString*)date :(NSString*)appid :(NSString*)key
{
NSString *API = [[NSString alloc]
initWithFormat:@";type=%@date=%@appid=%@key=%@",
areaid, type, [date substringToIndex:12], [appid substringToIndex:6],
key];
//-------------这里需要主要的是只需要appid的前6位!!!
return
API;
}
//将获得的key进性urlencode操作
- (NSString
*)stringByEncodingURLFormat:(NSString*)_key{
NSString *encodedString
= (__bridge NSString
*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)_key,
nil, (CFStringRef) @"!$'()*+,-./:;=?@_~%#[]",
kCFStringEncodingUTF8);
//由于ARC的存在,这里的转换需要添加__bridge,原因我不明。求大神讲解
return
encodedString;
}
重点来了,在oc下的算法如下,记得把附件的Base64.h
加进来并引用到工程里面
//对publickey和privatekey进行加密
- (NSString *)
hmacSha1:(NSString*)public_key :(NSString*)private_key{
NSData*
secretData = [private_key
dataUsingEncoding:NSUTF8StringEncoding];
NSData* stringData = [public_key
dataUsingEncoding:NSUTF8StringEncoding];
const void* keyBytes =
[secretData bytes];
const void* dataBytes = [stringData
bytes];
///#define CC_SHA1_DIGEST_LENGTH 20 /* digest
length in bytes */
void* outs =
malloc(CC_SHA1_DIGEST_LENGTH);
CCHmac(kCCHmacAlgSHA1, keyBytes,
[secretData length], dataBytes, [stringData length], outs);
//
Soluion 1
NSData* signatureData = [NSData dataWithBytesNoCopy:outs
length:CC_SHA1_DIGEST_LENGTH freeWhenDone:YES];
return
[signatureData
base64EncodedString];
}
这里只是初步算出来的key,还未encodeurl,链接不能被浏览器识别,所以现在经过算法得到的_key还有一步操作才能的到真正的key。
NSString *_key = [getTime hmacSha1:[getTime
getPublicKey:areaid :type :date :appid] :private_key];
NSString *key =
[getTime
stringByEncodingURLFormat:_key];
最后一步了吧!拼接API
NSString *weatherAPI = [getTime getAPI:areaid :type :date
:appid
:key];
//OK,我们的API就可以用啦。
最后,通过API返回的值是 *** ON文件,通过解析,就能得到我们想要的数据了,下面拿一个开发的接口举例
NSDictionary *weatherDic = [getTime
getWeatherDic:@""];
// weatherDic字典中存放的数据也是字典型,从它里面通过键值取值
NSDictionary
*weatherInfo = [weatherDic
objectForKey:@"weatherinfo"];
NSLog(@"今天是 %@ %@ %@ 的天气状况是:%@ %@ -
%@",[newDateOne substringWithRange:N *** akeRange(0, 4)],[newDateOne
substringWithRange:N *** akeRange(4, 2)] ,[newDateOne
substringWithRange:N *** akeRange(6, 2)],[weatherInfo
objectForKey:@"weather"],[weatherInfo objectForKey:@"temp1"],[weatherInfo
objectForKey:@"temp2"]);
输出:2014-09-04 23:40:23.243
WeatherAPP[5688:201108] 今天是 2014-09-04 的天气状况是:晴 17℃ - 30℃
weatherInfo字典里面的内容是---{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"17℃","temp2":"30℃","weather":"晴","img1":"n0.gif","img2":"d0.gif","ptime":"18:00"}}
中国国家气象局提供了获取所在城市天气预报信息接口。通过这个接口,我们就可以获取天气信息了。
关于获取天气预报的实例参考android学习手册,里面有源码。android学习手册包含9个章节,108个例子,源码文档随便看,例子都是可交互,可运行,源码采用android studio目录结构,高亮显示代码,文档都采用文档结构图显示,可以快速定位。360手机助手中下载,图标上有贝壳
中国国家气象局天气预报接口总共提供了三个:
注:上面接口2014.3.4日已不再更新。换成这个:。各位看34楼。在此感谢tdwll和黄晓佳cobish。
最详细的信息来自第三个接口。上面url中的101010100是城市代码,这里是北京的城市代码。只需要改变城市代码,就可以得到所在城市的天气信息。笔者在福州,所以选择的城市代码是福州101230101。
在浏览器上输入url:得到信息,天气信息是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":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉","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":"不易发"}}
我们可以解析json数据去得到自己想用的天气信息。
天气信息解释:
[html] view plain copy print?
{
"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":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉",
!-- 天气描述 --
"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":"不易发"
}
}
{
"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":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉",
!-- 天气描述 --
"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":"不易发"
}
}
据我所知,气象局提供三个天气查询接口,如下:
1、实时天气接口
地址: sk/【城市代码】.html
例如:查询北京实时天气:
结果:
{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"13","WD":"南风","WS":"2","SD":"59%","WSE":"2","time":"10:30","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}}
2、今日天气接口
地址:【城市代码】.html
例如:查询北京今日天气:
结果:
{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"15","temp2":"5℃","weather":"阵雨转阴","img1":"d3.gif","img2":"n2.gif","ptime":"08:00"}}
3、今日及未来天气接口【内容最详细】
地址:【城市代码】.html
例如:查询北京今日及未来天气:
结果:
{"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2013年10月22日","date":"",
"week":"星期二","fchh":"08","cityid":"101010100","temp1":"15℃~5℃","temp2":"15℃~4℃……
…【省略】"index_tr":"适宜","index_co":"较舒适",
"st1":"11","st2":"1","st3":"13","st4":"5","st5":"14","st6":"4","index_cl":"较不宜",
"index_ls":"不太适宜","index_ag":"不易发"}}
参考:
Google已经为Windows、Mac和Linux用户发布了Chrome 99.0.4844.84,以解决一个在外部被利用的高严重性零日漏洞。浏览器供应商在周五发布的安全公告中说:”Google已经得知CVE-2022-1096的一个漏洞存在于外部。” 99.0.4844.84版本已经在稳定的桌...
在周四的一份网络安全公告中,育碧(Ubisoft)证实该公司在上周遭遇了一起“网络安全事件”。尽管攻击尝试似乎未能造成破坏,但出于安全方面的考虑,育碧还是采取了全公司范围内的密码重置措施,以防发生其它意外。在此期间,育碧暂停了部分服务,但坚称没有玩家数据受到损害。截止发稿时,该公司旗下所有游戏和服...
2018年,英特尔、AMD、ARM曝出CPU安全事件,引起广泛关注,舆论一片哗然。虽然英特尔公司表示此次事件不仅仅是英特尔,还涉及AMD/ARM等厂商,且CPU 漏洞补丁基本不会给普通用户造成任何影响,但这次bug依旧被定为成行业大事件。 时隔几年,CPU又再次曝出一个大bug,有意思的...
Hackernews 编译,转载请注明出处: 一个P2P的Golang僵尸网络在一年多后重新浮出水面,在一个月内侵入了医疗、教育和政府部门实体的服务器,感染了总共1500台主机。 Akamai 的研究人员在与 The Hacker News 共享的一份报告中称,这种名为 FritzFrog 的“分...
SushiSwap 首席技术官表示,该公司的 MISO 平台近日受到了软件供应链的攻击。SushiSwap 是一个社区驱动的去中心化金融(DeFi)平台,方便用户交换、赚取、借出、借用和利用加密货币资产。今年早些时候,Sushi 的最新产品 Minimal Initial SushiSwap Off...
荷兰当局向一家DDoS网站的十多名客户发出了最后通牒,通知他们,如果继续网络攻击将被起诉。 荷兰警方的信件旨在减少网络犯罪,并引导罪犯采用合法手段来提高他们的技能。 周一,29名荷兰公民收到了警方的信件,并得知他们的犯罪活动已经被记录,未来的犯罪行为可能会被定罪。 “我们已经在我们的系统中标记了你...