利用AFNetworking框架去管理从聚合数据上面请求到的数据
数据从JSON文档中读取处理的过程称为“解码”过程,即解析和读取过程,来看一下如果利用AFNetworking框架去管理从聚合数据上面请求到的数据。 一、下载并导入AFNetworking框架这部分内容如果不了解的话可以点击iOS开发之网络编程篇六:AFNetworking3.0使用简介 二、聚合数据API-餐饮美食1.申请餐饮美食API:https://www.juhe.cn/docs/api/id/45 聚合数据API用法简介 2.请求体介绍 lng和lat参数表示对应的经纬度,用来确定你想要请求哪个地方的美食餐饮,key参数即为APPKEY,你申请该API以后可以到应用详细页查询,dtype参数是用来确定你想得到的数据是什么格式的,默认为JSON,如果想要xml格式就需要设置该参数。 3.返回值介绍 该餐饮美食API会返回上图所示的参数,可以根据实际需要来使用。 三、发送网络请求1.知道API需要请求的参数以后,就可以通过接口地址和API文档中给出的请求方式,请求到所需的数据。 2.根据餐饮美食API的接口文档我们知道需要用get方式去请求。 NSMutableDictionary *params=[NSMutableDictionary dictionary]; //在数组里面添加请求参数 params[@"key"] = @"987d0c7bd487209bd5bb4065b3d4fcc2"; params[@"lng"] = @"121.538123"; params[@"lat"] = @"31.677132"; //创建请求管理者 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; //发送请求,使用get,请求成功以后返回的数据会存放到responSEObject中 [manager GET:@"http://apis.juhe.cn/catering/query" parameters:params success:^(NSURLSessionDataTask *task,id responSEObject) { //在请求成功以后程序会执行success //此处可以对请求到的数据进行处理 } failure:^(NSURLSessionDataTask *task,NSError *error) { }]; } 四、解析返回的数据1.我们发送网络请求以后会返回下图所示的数据: 从图中我们可以看到发送请求以后返回了一个字典,字典中result关键字对应的数组就是我们想要得到的数据,我们项目中需要用到该数组中navigation参数对应的数据,那我就可以遍历result关键字对应的数组,找到navigation参数对应的数据,然后将其存放到一个新的数组中。 [manager GET:@"http://apis.juhe.cn/catering/query" parameters:params success:^(NSURLSessionDataTask *task,id responSEObject) { //新建一个字典来存放返回的数据 NSMutableDictionary *datasouce=[responSEObject mutableCopy]; //在返回的字典中,将关键字result索引到的数据存放到另外的数组中 NSArray * resultArray = [datasouce objectForKey:@"result"]; //遍历resultArray数组得到navigation对应的数据,并存放到result数组中 for (NSDictionary *dic in resultArray) { NSString *navigation = [dic objectForKey:@"navigation"]; [result addObject:navigation]; } [self.delegate data:result]; } failure:^(NSURLSessionDataTask *task,NSError *error) { NSLog(@"%@",error); }]; } 2.创建一个继承于NSObject的Data类,用来得到从聚合数据上面请求到的数据,并在Data.h文件中添加相应的属性方法 -(void) getdata;//在ViewController中执行此方法从而得到JSON数据 3.在Data.m文件中完成相应的属性方法 -(void) getdata{ __block NSMutableArray *result = [[NSMutableArray alloc] init]; NSMutableDictionary *params=[NSMutableDictionary dictionary]; //在数组里面添加请求参数,根据聚合数据的文档说明添加 params[@"key"] = @"987d0c7bd487209bd5bb4065b3d4fcc2"; params[@"lng"] = @"121.538123"; params[@"lat"] = @"31.677132"; //创建请求管理者 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; //发送请求 [manager GET:@"http://apis.juhe.cn/catering/query" parameters:params success:^(NSURLSessionDataTask *task,id responSEObject) { //如果数据请求成功返回到responSEObject中 NSMutableDictionary *datasouce=[responSEObject mutableCopy]; //在返回的字典中通过关键字result索引到的数据存放到另外的数组中 NSArray * resultArray = [datasouce objectForKey:@"result"]; //遍历resultArray数组得到navigation对应的数据 for (NSDictionary *dic in resultArray) { NSString *navigation = [dic objectForKey:@"navigation"]; [result addObject:navigation]; } [self.delegate data:result]; } failure:^(NSURLSessionDataTask *task,error); }]; } 4.创建一个DataDelegate协议,并添加相关的代理方法,用来传递JSON数据。 #import <Foundation/Foundation.h> @protocol DataDelegate //通过代理将得到的JSON数据传递到ViewController - (void) data: (NSMutableArray *)array; @end @interface Data : NSObject @property (nonatomic,weak) id <DataDelegate> delegate;//代理属性 -(void) getdata; @end 五、在TableView上面显示得到的JSON数据1.添加相关的属性以及代理协议 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,DataDelegate> @property (nonatomic,strong) UITableView *myTableView; @property (nonatomic,strong) NSArray *myArray;//用来存放JSON数据的数组 @property (nonatomic,strong) Data *myData; @end 2.使懒加载方式初始化所需的控件以及数组 - (Data *)myData{ if (!_myData) { _myData = [[Data alloc] init]; _myData.delegate = self; [_myData getdata]; } return _myData; } - (NSArray *) myArray{ if (!_myArray) { _myArray = [[NSArray alloc ] init]; } return _myArray; } - (UITableView *) myTableView{ if (!_myTableView) { _myTableView = [[UITableView alloc] initWithFrame:self.view.frame]; _myTableView.dataSource = self; _myTableView.delegate = self; [self.view addSubview: _myTableView]; } return _myTableView; } - (void)viewDidLoad { [super viewDidLoad]; [self myArray]; [self myData]; [self myTableView]; } 3.在tableView的数据源方法中将得到的JSON数据显示到tableView上面 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.myArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } if (self.myArray != nil && self.myArray.count > 0) { cell.textLabel.text = [self.myArray objectAtIndex:[indexPath row]]; } return cell; } 4.由于tableView的数据源方法会先执行,因此我们需要在Data的代理方法中得到JSON数据以后刷新tableView。 - (void)data:(NSMutableArray *)array{ self.myArray = array; [self.myTableView reloadData]; } 示例代码http://pan.baidu.com/s/1skJUHPF (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |