聚合數據iOS SDK 全國天氣預報演示示例

1,將聚合數據SDK(JuheApis.framework)添加到你的程序中來,SDK依賴的包包括:



2,在聚合SDK Framework的JHSDKAPIPath.h文件中找到快遞查詢接口宏,以及字典參數

/*** 全國天氣預報 ***/

/**
@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 :juhe.apis.weather.index (宏 : kJHAPIS_LIFE_WEATHER_INDEX ) 

返回字段 :

wKioL1Q4pVTCVXK5AANdHVfHoUQ335.jpg

wKiom1Q4pR2x8tuxAADfdlEQDjQ212.jpg

2. 天氣種類及標識列表

API :juhe.apis.weather.uni (宏 :kJHAPIS_LIFE_WEATHER_UNI )

返回字段 : 

wKioL1Q4pVSyfv74AADfWAUJ0-g462.jpg

3. 根據IP查詢天氣 

API :juhe.apis.weather.ip (宏 :kJHAPIS_LIFE_WEATHER_IP )

返回字段同juhe.apis.weather.index(根據城市名/id查詢天氣)。


4. 根據GPS座標查詢天氣

API :juhe.apis.weather.geo (宏 :kJHAPIS_LIFE_WEATHER_GEO)

返回字段同juhe.apis.weather.index(根據城市名/id查詢天氣)。


5. 城市天氣三小時預報

API :juhe.apis.weather.forecast3h (宏 :kJHAPIS_LIFE_WEATHER_FORECAST3H )

返回字段 :

wKioL1Q4paTwE2BwAAHbtGtoywM637.jpg

6. 支持城市列表

API :juhe.apis.weather.citys (宏 :kJHAPIS_LIFE_WEATHER_CITYS )

返回字段 : 

wKiom1Q4pW3ThWpZAAEat5V2A10698.jpg

7 . 全國天氣預報錯誤碼

wKioL1Q4paTiT3aQAAEktV73ULQ242.jpg

5, 更多聚合數據SDK接口,訪問這裏: http://www.juhe.cn/juhesdk/idocs


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章