您的位置:首页 > 理论基础 > 计算机网络

iOS网络请求总结

2015-04-20 17:09 218 查看
*说明:文章中HTTP为宏定义的http地址,事例通过app_login.action的接口,通过传递policyNum、plateNum、phoneNum三个参数进行登录操作

一、方法1: Foundation框架 NSURLConnection

(1)同步请求:同步方法加载url请求,会阻塞当前线程,一般不使用。代码如下

#pragma mark - 同步网络请求
-(void)SynchronousRequest{
    NSString *url = [[NSString alloc] initWithFormat:@"%@app_login.action?policyNum=111111&plateNum=222222&phoneNum=333333",HTTP];//请求的url地址
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];//根据url地址创建一个请求
    
    NSURLResponse *response = nil;//请求答复
    NSError *error = nil;//请求错误信息
    NSData *content = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];//使用NSURLConnection发起一个请求,用content保存请求结果
    
    NSString *string = [[NSString alloc]initWithData:content encoding:NSUTF8StringEncoding];//将请求结果转成String
    
    NSLog(@"response:%@",string);//打印请求结果
}


(2)异步请求:异步方法加载url请求,不会造成当前线程阻塞,通过实现代理

NSURLConnectionDelegate的方法,实现结果数据处理,另外也可以通过block的方式处理结果数据, 代理方式代码如下:

#pragma mark - 异步网络请求
-(void)AsynchronousRequest{
    NSString *url = [[NSString alloc] initWithFormat:@"%@app_login.action?policyNum=111111&plateNum=222222&phoneNum=333333",HTTP];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    aSynConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"didFailWithError");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    if( [connection isEqual: aSynConnection])
    {
        NSString *asyReturn = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSLog(@"%@",asyReturn);
        responseData = nil;
    }
}


二.方法2:通过第三方框架:AFNetworking
(1)从GitHub下载AFNetworking:https://github.com/AFNetworking/AFNetworking

(2)将AFNetworking
文件夹拖到工程中

(3)在需要的地方#import
"AFNetworking.h"

AFNetworking已经给我们封装好了异步网络请求,主要代码如下:

#pragma mark - AFNetworking 请求
-(void)afNetworkingPost{
    NSURL *url = [NSURL URLWithString:HTTP];
    NSDictionary *parameters = @{@"policyNum":@"PDAT20155001T000037476", @"plateNum":@"渝A85***",@"phoneNum":@"1888335****"};//参数列表
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    [manager POST:@"app_login.action" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"success: %@",responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error: %@, \n error.localizedDescription: %@", error, [error localizedDescription]);
    }];
}


三.加载网络图片:通过第三方框架SDWebImage

(1)从GitHub下载SDWebImage:https://github.com/rs/SDWebImage

(2)将SDWebImage 文件夹拖到工程中

(3)在需要的地方#import "SDWebImage/UIImageView+WebCache.h"

例如从网络加载TableViewCell中的图片,只要在TableView的Delegate中使用SDWebImage的sd_setImageWithURL:(NSURL *)url方法,主要代码如下:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
[cell.image sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"pic"]];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: