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

目前可能只是单纯的贴上了一些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];
}




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。