利用AFNetworking框架去管理从聚合数据上面请求到的数据

来源:互联网 发布:c语言两个数组相加 编辑:程序博客网 时间:2024/05/16 06:46

数据从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关键字对应的数组就是我们想要得到的数据,我们项目中需要

[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, NSError *error) {        NSLog(@"%@",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











0 0