做一个APP让我写代码不难,难的是一些复杂的资源,比如说,图片、接口
1、接口准备:
其实小Q作为一款工具类型的APP,需要的接口信息并不难,
天气部分:
在百度的APIStore中都可以找到天气预报的接口,地址如下
http://apistore.baidu.com/
我申请的天气服务是一个免费版的 中国和世界天气预报 地址如下
http://apistore.baidu.com/apiworks/servicedetail/478.html
API简介 :
Paste_Image.png
API的简介连接里说的很详细了,也包括了OC调用方法,(代码如下)
NSString *httpUrl = @"http://apis.baidu.com/heweather/pro/weather";
NSString *httpArg = @"city=beijing";
[self request: httpUrl withHttpArg: httpArg];
-(void)request: (NSString*)httpUrl withHttpArg: (NSString*)HttpArg
{
NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, HttpArg];
NSURL *url = [NSURL URLWithString: urlStr];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
[request setHTTPMethod: @"GET"];
[request addValue: @"您自己的apikey" forHTTPHeaderField: @"apikey"];
[NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
if (error) {
NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
} else {
NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HttpResponseCode:%ld", responseCode);
NSLog(@"HttpResponseBody %@",responseString);
}
}];
返回json如下:
{ "HeWeather data service 3.0": [{ "status": "ok", //接口状态 "alarms": [{ //灾害预警,若所在城市无预警则不显示该字段,仅限国内城市 "level": "黄色", //预警等级 "stat": "预警中", //预警状态 "title": "北京市怀柔区气象台发布暴雨黄色预警", //预警信息标题 "txt": "目前怀柔北部山区局地降雨量已达35毫米,预计未来1至2小时降雨仍将持续,局地累计雨量将达到50到70毫米,山区请注意防范地质灾害。", //预警信息详情 "type": "暴雨 " //预警天气类型 }], "basic": { //基本信息 "city": "北京", //城市名称 "cnty": "中国", //国家 "id": "CN101010100", //城市ID,所有城市ID请参见 http://www.heweather.com/documents/cn-city-list "lat": "39.904000", //城市维度 "lon": "116.391000", //城市经度 "update": { //更新时间 "loc": "2015-07-02 14:44", //当地时间,如无特殊说明,以下时间均为当地时间 "utc": "2015-07-02 06:46" //UTC时间 } }, "now": { //实况天气 "cond": { //天气状况 "code": "100", //天气状况代码,所有天气代码和中英文对照以及图标请参见http://www.heweather.com/documents/condition-code "txt": "晴" //天气状况描述 }, "fl": "30", //体感温度 "hum": "20%", //相对湿度(%) "pcpn": "0.0", //降水量(mm) "pres": "1001", //气压 "tmp": "32", //温度 "vis": "10", //能见度(km) "wind": { //风力风向 "deg": "10", //风向(360度) "dir": "北风", //风向 "sc": "3级", //风力 "spd": "15" //风速(kmph) } }, "aqi": { //空气质量,仅限国内城市 "city": { "aqi": "30", //空气质量指数 "co": "0", //一氧化碳1小时平均值(ug/m³) "no2": "10", //二氧化氮1小时平均值(ug/m³) "o3": "94", //臭氧1小时平均值(ug/m³) "pm10": "10", //PM10 1小时平均值(ug/m³) "pm25": "7", //PM2.5 1小时平均值(ug/m³) "qlty": "优", //空气质量类别 "so2": "3" //二氧化硫1小时平均值(ug/m³) } }, "daily_forecast": [ //天气预报,国内7天,国际10天 { "date": "2015-07-02", //预报日期 "astro": { //天文数值 "sr": "04:50", //日出时间 "ss": "19:47" //日落时间 }, "cond": { //天气状况 "code_d": "100", //白天天气状况代码 "code_n": "100", //夜间天气状况代码 "txt_d": "晴", //白天天气状况描述 "txt_n": "晴" //夜间天气状况描述 }, "hum": "14", //相对湿度(%) "pcpn": "0.0", //降水量(mm) "pop": "0", //降水概率 "pres": "1003", //气压 "tmp": { //温度 "max": "34", //最高温度 "min": "18" //最低温度 }, "vis": "10", //能见度(km) "wind": { //风力风向 "deg": "339", //风向(360度) "dir": "东南风", //风向 "sc": "3-4", //风力 "spd": "15" //风速(kmph) } }, ...... //略 ], "hourly_forecast": [ //当天每小时天气预报 { "date": "2015-07-02 01:00", //时间 "hum": "43", //相对湿度(%) "pop": "0", //降水概率 "pres": "1003", //气压 "tmp": "25", //温度 "wind": { //风力风向 "deg": "320", //风向(360度) "dir": "西北风", //风向 "sc": "微风", //风力 "spd": "12" //风速(kmph) } }, ...... //略 ], "suggestion": { //生活指数,仅限国内城市 "comf": { //舒适度指数 "brf": "较不舒适", //简介 "txt": "白天天气多云,同时会感到有些热,不很舒适。" //详细描述 }, "cw": { //洗车指数 "brf": "较适宜", "txt": "较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。" }, "drsg": { //穿衣指数 "brf": "炎热", "txt": "天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。" }, "flu": { //感冒指数 "brf": "少发", "txt": "各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。" }, "sport": { //运动指数 "brf": "较适宜", "txt": "天气较好,户外运动请注意防晒。推荐您进行室内运动。" }, "trav": { //旅游指数 "brf": "较适宜", "txt": "天气较好,温度较高,天气较热,但有微风相伴,还是比较适宜旅游的,不过外出时要注意防暑防晒哦!" }, "uv": { //紫外线指数 "brf": "中等", "txt": "属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。" } } }] }
API错误码
限制类错误.png
调用方法错误.png
代理平台错误.png
特殊处理:
因为本条API为免费版,所以API方做了一些限制(图1),
Paste_Image.png
每天6000次,每秒请求30次,为了避免并发使用量过大,我注册了三个账号,得到了三个APPKEY,然后通过一个随机数,随机选择APPKEY,最为参数请求,这样几乎可以每秒并发90次,每天请求18000次(我这样算不对对,对吧)
雾霾数据:
雾霾数据的API也是使用一个免费的 空气质量指数 连接如下
http://apistore.baidu.com/apiworks/servicedetail/116.html
使用方法如下:
Paste_Image.png
OC调用事例
NSString *httpUrl = @"http://apis.baidu.com/apistore/aqiservice/citylist";
NSString *httpArg = @"";
[self request: httpUrl withHttpArg: httpArg];
-(void)request: (NSString*)httpUrl withHttpArg: (NSString*)HttpArg {
NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, HttpArg];
NSURL *url = [NSURL URLWithString: urlStr];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
[request setHTTPMethod: @"GET"];
[request addValue: @"您自己的apikey" forHTTPHeaderField: @"apikey"];
[NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
if (error) {
NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
} else {
NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HttpResponseCode:%ld", responseCode);
NSLog(@"HttpResponseBody %@",responseString);
}
}];
}
错误码如下:
Paste_Image.png
Paste_Image.png
Paste_Image.png
本套餐限制较少
原文地址,http://mp.weixin.qq.com/s/du8atCfrD9SNpPSa13yb9g 感谢分享