1,将聚合数据SDK(JuheApis.framework)添加到你的程序中来,SDK依赖的包包括:
/*** 全国天气预报 ***/
@brief 全国天气 -> 根据城市名/id查询天气
@param cityname string 必填 城市名或城市ID,如:"苏州"
@param dtype string 必填 返回数据格式:json或xml,默认json
@param format int 非必填 未来6天预报(future)两种返回格式,1或2,默认1
*/
#define kJHAPIS_LIFE_WEATHER_INDEX @"juhe.apis.weather.index" //1、根据城市名/id查询天气
/**
@brief 全国天气 -> 天气种类及标识列表
@param dtype string 非必填 返回数据的格式,xml或json,默认json
*/
#define kJHAPIS_LIFE_WEATHER_UNI @"juhe.apis.weather.uni" //2、天气种类及标识列表
/**
@brief 全国天气 -> 根据IP查询天气
@param ip string 必填 ip地址,如:58.215.185.154
@param dtype string 必填 返回数据格式:json或xml,默认json
@param format int 非必填 未来6天预报(future)两种返回格式,1或2,默认1
*/
#define kJHAPIS_LIFE_WEATHER_IP @"juhe.apis.weather.ip" //3、根据IP查询天气
/**
@brief 全国天气 -> 根据GPS坐标查询天气
@param lon string 必填 经度,如:116.39277
@param lat string 必填 纬度,如:39.933748
@param format int 非必填 未来6天预报(future)两种返回格式,1或2,默认1
@param dtype string 必填 返回数据格式:json或xml,默认json
*/
#define kJHAPIS_LIFE_WEATHER_GEO @"juhe.apis.weather.geo" //4、根据GPS坐标查询天气
/**
@brief 全国天气 -> 城市天气三小时预报
@param cityname string 必填 城市名,如:"苏州"
@param dtype string 必填 返回数据格式:json或xml,默认json
*/
#define kJHAPIS_LIFE_WEATHER_FORECAST3H @"juhe.apis.weather.forecast3h" //5、城市天气三小时预报
/**
@brief 全国天气 -> 支持城市列表
@param dtype string 非必填 返回数据的格式,xml或json,默认json
*/
#define kJHAPIS_LIFE_WEATHER_CITYS @"juhe.apis.weather.citys" //6、支持城市列表
3,全国天气预报接口在程序中调用方法(将ViewController.m改为.mm)
#import "ViewController.h"
#import <JuheApis/JuheAPI.h>#import <JuheApis/JHOpenidSupplier.h>
#import <JuheApis/JHSDKAPIPath.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[JHOpenidSupplier shareSupplier] registerJuheAPIByOpenId:@"申请到的OpenId“];
UIButton* beginBtn=[UIButton buttonWithType:UIButtonTypeSystem];
beginBtn.frame=CGRectMake(20, 111, 280, 40);
[beginBtn setTitle:@"开始" forState:UIControlStateNormal];
[beginBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[beginBtn addTarget:self action:@selector(doTestAction) forControlEvents:UIControlEventTouchUpInside];
[beginBtn setBackgroundImage:[UIImage imageNamed:@"button5"] forState:UIControlStateNormal];
[self.view addSubview:beginBtn];
}
- (void)doTestAction
{
/* 1. 根据城市名/id查询天气 */
[self test : kJHAPIS_LIFE_WEATHER_INDEX parameters:@{@"cityname":@"苏州" , @"dtype":@"json" } ];
/* 2. 天气种类及标识列表 */
[self test : kJHAPIS_LIFE_WEATHER_UNI parameters:@{@"dtype":@"json"} ];
/* 3. 根据IP查询天气 */
[self test : kJHAPIS_LIFE_WEATHER_IP parameters:@{@"ip":@"58.215.185.154" , @"dtype":@"json"} ];
/* 4. 根据GPS坐标查询天气 */
[self test : kJHAPIS_LIFE_WEATHER_GEO parameters:@{@"lon":@"116.39277" , @"lat":@"39.933748" , @"dtype":@"json" } ];
/* 5. 城市天气三小时预报 */
[self test : kJHAPIS_LIFE_WEATHER_FORECAST3H parameters:@{@"cityname":@"苏州" , @"dtype":@"json" } ];
/* 6. 支持城市列表 */
[self test : kJHAPIS_LIFE_WEATHER_CITYS parameters:@{@"dtype":@"json" } ];
}
- (void)test:(NSString *)path parameters:(NSDictionary *)parameters{
JuheAPI *juheapi = [JuheAPI shareJuheApi];
[juheapi executeWorkWithAPI:path
parameters:parameters
success:^(id responseObject){
if ([[parameters objectForKey:@"dtype"] isEqualToString:@"xml"]) {
NSLog(@"***xml*** \n %@", responseObject);
}else{
int error_code = [[responseObject objectForKey:@"error_code"] intValue];
if (!error_code) {
NSLog(@" %@", responseObject);
}else{
NSLog(@" %@", responseObject);
}
}
} failure:^(NSError *error) {
NSLog(@"error: %@",error.description);
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
4,全国天气预报接口返回数据说明以及错误码说明
1. 根据城市名/id查询天气
API : kJHAPIS_LIFE_WEATHER_INDEX
返回字段 :
名称 |
|
| 类型 | 说明 |
resultcode |
|
| int | 返回码 |
reason |
|
| string | 返回说明 |
result |
|
| dictionary | 返回结果 |
sk |
|
| dictionary | 当前实况天气 |
| temp |
| int | 当前温度 |
| wind_direction |
| string | 当前风向 |
| wind_strength |
| string | 当前风力 |
| humidity |
| string | 当前湿度 |
| time |
| date | 更新时间 |
today |
|
| dictionary | 今日天气 |
| city |
| string | 城市 |
| date_y |
| string | 日期 |
| week |
| string | 星期 |
| temperature |
| string | 今日温度 |
| weather |
| string | 今日天气 |
| weather_id |
| dictionary | 天气唯一标识 |
|
| fa | Int | 天气标识00:晴 |
|
| fb | int | 天气标识53:霾 如果fa不等于fb,说明是组合天气 |
| wind |
| string | 风向与风力 |
| dressing_index |
| string | 穿衣指数(较冷) |
| dressing_advice |
| string | 穿衣建议 |
| uv_index |
| string | 紫外线强度 |
| comfort_index |
| string | 舒适度指数 |
| wash_index |
| string | |
| travel_index |
| string | 旅游指数 |
| |
| string | |
| |
| string | |
|
|
| array | |
|
|
| int | |
2. 天气种类及标识列表
API : kJHAPIS_LIFE_WEATHER_UNI
返回字段 :
名称 |
| 类型 | 说明 |
resultcode |
| int | 返回码 |
reason |
| string | 返回说明 |
result |
| array | 返回结果集 |
| wid | string | 天气唯一标示符 |
| weather | string | 天气 |
error_code |
| int | 错误码 |
3. 根据IP查询天气
API : kJHAPIS_LIFE_WEATHER_IP
返回字段同juhe.apis.weather.index(根据城市名/id查询天气)。
4. 根据GPS坐标查询天气
API : kJHAPIS_LIFE_WEATHER_GEO
返回字段同juhe.apis.weather.index(根据城市名/id查询天气)。
5. 城市天气三小时预报
API : kJHAPIS_LIFE_WEATHER_FORECAST3H
返回字段 :
名称 |
| 类型 | 说明 |
resultcode |
| int | 返回码 |
reason |
| string | 返回说明 |
result |
| array | 返回结果集 |
| | int | |
| | string | |
| | string | 低温 |
| | string | 高温 |
| | string | 开始小时 |
| | string | 结束小时 |
| wid | string | 天气唯一标示符 |
| | string | |
| | string | |
| | string | |
error_code |
| int | 错误码 |
6. 支持城市列表
API : kJHAPIS_LIFE_WEATHER_CITYS
返回字段 :
名称 |
| 类型 | 说明 |
resultcode |
| int | 返回码 |
reason |
| string | 返回说明 |
result |
| dictionary | 返回结果集 |
| | int | |
| province | string | 省份名称 |
| city | string | 城市名称 |
| | string | |
error_code |
| int | 错误码 |
7 . 全国天气预报错误码
| 错误码 | 说明 |
| 203901 | 查询城市不能为空 |
| 203902 | 查询不到该城市的天气 |
| 203903 | 查询出错,请重试 |
| 203904 | 错误的GPS坐标 |
| 203905 | GPS坐标解析出错,请确认提供的坐标正确(暂支持国内) |
| 203906 | IP地址错误 |
| 203907 | 查询不到该IP地址相关的天气信息 |
5, 更多聚合数据SDK接口,访问这里: http://www.juhe.cn/juhesdk/idocs