码迷,mamicode.com
首页 > 移动开发 > 详细

ios之NSURLConnection网络请求数据/GET与POST方法

时间:2014-12-20 11:46:51      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:网络请求

目前可能只是单纯的贴上了一些demo的代码,但是这些代码都是请求数据应该是最基础的使用方法吧,在项目的实际开发中可能用到系统的会非常少,一般都是采用别人非常成熟的第三方开源库来实现数据请求,目前常用的第三方网络请求主要是以下几个:<h3 style="margin: 18px 0px; padding: 0px 0px 5px; border: 0px; outline: 0px; font-size: 22px; vertical-align: baseline; color: rgb(51, 51, 51); line-height: 1em; font-weight: normal; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">HTTP</h3><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; line-height: 26px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">相比较之下,<a target=_blank href="https://github.com/AFNetworking/AFNetworking" target="_blank" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; text-decoration: none; color: rgb(75, 182, 245); background: transparent;">AFNetworking</a>是目前最优秀的一个了:轻量、易用、使用者多、开发者有在积极维护。在AFN出现之前,这个角色是由<a target=_blank href="https://github.com/pokeb/asi-http-request" target="_blank" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; text-decoration: none; color: rgb(75, 182, 245); background: transparent;">ASIHTTPRequest</a>扮演的,只是到现在年久失修了。关于AFN和ASI的对比,这里有一篇不错的文章<a target=_blank href="http://www.infoq.com/cn/articles/afn_vs_asi" target="_blank" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; text-decoration: none; color: rgb(75, 182, 245); background: transparent;">http://www.infoq.com/cn/articles/afn_vs_asi</a>。除此之外,<a target=_blank href="https://github.com/MugunthKumar/MKNetworkKit" target="_blank" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; text-decoration: none; color: rgb(75, 182, 245); background: transparent;">MKNetworkKit</a>和<a target=_blank href="https://github.com/RestKit/RestKit" target="_blank" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; text-decoration: none; color: rgb(75, 182, 245); background: transparent;">RestKit</a>也有一定的使用者。</p>
#pragma mark -- NetWorking methods

// 分别是get的同步和异步
- (void)getHttpRequest
{
    
    NSString *urlString = [NSString stringWithFormat:@"http://api.jiepang.com/v1/locations/search?lat=%f&lon=%f&source=100000&count=50",30.575413,104.064359];
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    /*
    NSError *error = nil;
    // 这里是发布同步请求,同步请求会阻塞主线程,在请求未完成过程中程序是无法进行交互的
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    if (error) {
        NSLog(@"request error reason '%@'",[error localizedDescription]);
    }else
    {
     //进行数据解析,这里是json解析采用的系统自带的
        id objId = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        _weatherLabel.text = objId[@"result"][@"today"][@"weather"];
        NSLog(@"%@",objId);
    }
   */
    
    // 这里是通过代理来实现异步请求 需要实现其代理方法去获取数据
    [NSURLConnection connectionWithRequest:request delegate:self];
}

// post异步请求
- (void)postHttpRequest
{
    NSURL *url = [NSURL URLWithString:OIL_PRICE];
    // 对参数进行处理
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:@"5a43794d639815615b93fdfddcef3670" forKey:@"key"];
    
    NSMutableString *mstring = [NSMutableString string];
    for (NSString *key in [params allKeys]) {
        [mstring appendFormat:@"%@=%@",key,params[key]];
    }
    // POST 请求需要可变的URLRequest
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    request.HTTPBody = [mstring dataUsingEncoding:NSUTF8StringEncoding]; // 请求参数进行编码
    request.HTTPMethod = @"POST"; // 请求方式
    request.timeoutInterval = 10;//请求超时时间
    [NSURLConnection connectionWithRequest:request delegate:self];
    
}
<pre name="code" class="objc">#pragma  mark -- <NSURLConnectionDelegate>

// 请求完毕调用此方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *error = nil;
    id objID = [NSJSONSerialization JSONObjectWithData:_receiveData options:NSJSONReadingMutableLeaves error:&error];
    if (error) {
        NSLog(@"reciveData error reason : '%@'.",[error localizedDescription]);
    }else
    {
       
        _dataSource = [objID[@"items"] mutableCopy];
        
        [_tableView reloadData];
       
    }
}
// 数据流接收方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // 接受数据
    [_receiveData appendData:data];
}




ios之NSURLConnection网络请求数据/GET与POST方法

标签:网络请求

原文地址:http://blog.csdn.net/mr_rog/article/details/42040775

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!