[第三方]AFNetWorking3.0网络框架使用方法

官网地址https://github.com/AFNetworking/AFNetworking

#import <AFNetworking.h>

- (void)viewDidLoad {

    //GET方法
    //创建查询字符串
    NSString* str=@"http://apis.juhe.cn/cook/query";
    //创建查询条件的字典
    NSDictionary* dic=@{@"key":@"912b2db2ecd52723f74c1b7fec8eca38",@"menu":@"红烧肉",@"rn":@"20",@"pn":@"1"};
    //创建配置文件
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    //用配置文件初始化AFURLSessionManager
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    //创建GET请求 如果需要POST请求 直接修改此处的Method
    NSURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:str parameters:dic error:nil];
    //创建数据任务
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            NSLog(@"%@",responseObject);
            NSDictionary* resultDic=[responseObject valueForKey:@"result"];
            array=[resultDic valueForKey:@"data"];
            NSLog(@"%@",array[0]);
            [table reloadData];
        }
    }];
    //开始数据任务
    [dataTask resume];

}

 

posted @ 2015-12-17 14:42  death3721  阅读(174)  评论(0编辑  收藏  举报