网络GET

 一. 同步

//1.创建网址字符串对象

    NSString *urlStr = [NSString stringWithFormat:@"http://api.map.baidu.com/place/v2/search?query=%@&region=%@&output=json&ak=6E823f587c95f0148c19993539b99295", @"大保健", @"郑州"];

//2.如果网址中出现中文, 需要进行URLEncode

    NSString *newuRLStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

//3.创建NSURL对象

    NSURL *url = [NSURL URLWithString:newuRLStr];

//4.创建请求对象(NSURLRequest)

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    NSURLResponse *response = nil; // 存储服务器响应信息

    NSError *error = nil; // 存储请求失败信息

//5.建立连接

    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

//解析

/**

 *  同步连接和异步连接之间的区别

 1.同步连接由主线程完成网络的请求任务, 在数据请求完毕之前, 所有用户交互无法处理, 会造成程序卡顿, 影响用户体验

 2.异步连接, 系统默认开辟子线程完成网络请求任务, 主线程依旧处理用户交互, 因此, 用户体验很好, 操作流畅 -- 采用以空间换时间.

 */

二.异步

//1.创建网址字符串对象

    NSString *urlStr = [NSString stringWithFormat:@"http://api.map.baidu.com/place/v2/search?query=%@&region=%@&output=json&ak=6E823f587c95f0148c19993539b99295", @"养生", @"郑州"];

    //2.如果网址中出现中文, 需要进行URLEncode

    NSString *newuRLStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    //3.创建NSURL对象

    NSURL *url = [NSURL URLWithString:newuRLStr];

    //4.创建请求对象(NSURLRequest)

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

//(1)block 方式

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        //解析

        ...

    }];

//(2)协议和代理

   self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

#pragma mark - NSURLConnectionDataDelegate

//当收到服务器响应时 触发

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    //当收到服务器响应时, 开辟空间(data)

    self.data = [NSMutableData data];

    

}

//当收到服务器传输的数据时 触发(可能会触发多次)

//当传输的数据比较大时, 服务器并不会将所有的数据全部传输过来, 可能每一次传输一部分, 每一次传输都会触发该方法

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    //当收到数据时, 拼接数据

    [self.data appendData:data];

}

//当收到服务器响传输的数据结束时 触发

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    [self analysisData:self.data];

}

//请求失败 触发

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    NSLog(@"失败了!");

}

#pragma mark -解析方法

三.解析

- (void)analysisData:(NSData *)data {

    

    [self.dataSource removeAllObjects];

    

    //6.解析

    //使用可变字典接受, 一放面, 将解析之后获得的是可变的容器, 另一方面, 得到的是最外层的字典, 因此, 使用可变字典接受数据

    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    //获取字典中的小数组

    NSArray *dataArray = dic[@"results"];

    //将数组中的字典对象转化为business 对象

    for (NSDictionary *dic in dataArray) {

        Business *business = [[Business alloc]init];

        //使用KVC 进行赋值

        [business setValuesForKeysWithDictionary:dic];

        //添加到数组

        [self.dataSource addObject:business];

        //  释放 释放 释放 释放 释放

        [business release];

    }

    [self.tableView reloadData];

}

posted @ 2015-09-18 14:34  kevin丶涛  阅读(134)  评论(0编辑  收藏  举报