[iOS 多线程 & 网络

A.ASI基本知识
1.ASI简单介绍
ASI:全称是ASIHTTPRequest,外号“HTTP终结者”,功能十分强大。
ASI的实现基于底层的CFNetwork框架,因此运行效率很高。
ASI的github地址
 
2.ASI的使用
(1)导入
  下载并导入ASI框架,注意该框架依赖于Reachability.
Image(51)
 
(2)ASI的源码需要设置为非ARC编译:
Image(52)
 
 
ASI依赖框架:
Image(53)
 
B.基本使用
创建请求对象 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
 
1.发送GET & POST请求
(1)GET请求
 1 /** get请求 */
 2 - (void)sendByGet{
 3     // 创建请求
 4     NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/login?user=tom&password=123"];
 5     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
 6    
 7     // 设置请求
 8     request.timeOutSeconds = 10; // 超时时间
 9     request.delegate = self;
10    
11     // 使用selector处理请求返回数据
12     [request setDidStartSelector:@selector(startRequest)];
13    
14     // 发送请求
15     [request startSynchronous]; // 同步请求
16 }
17 
18 /** 请求开始 */
19 - (void) startRequest
20     NSLog(@"请求开始")
21 }
 
(2)POST请求
使用ASIFormDataRequest
 1 - (void) sendByPost2 {
 2     // 创建请求
 3     NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/login"];
 4     self.formRequest = [ASIFormDataRequest requestWithURL:url];
 5    
 6     // 添加请求参数
 7     [self.formRequest addPostValue:@"tom" forKey:@"user"];
 8     [self.formRequest addPostValue:@"123" forKey:@"password"];
 9    
10     self.formRequest.completionBlock = ^ {
11         NSLog(@"请求完毕");
12     };
13    
14     // 发送请求
15     [self.formRequest startAsynchronous];
16 }
 
注意add和set的区别,一个是添加(适用于多值参数),一个是覆盖(内部先remove,再add)。
Image(54)
 
 
2.发送同步 & 异步请求
(1)发送同步请求
[request sendSynchronous];
(2)发送异步请求
[request sendAsynchronous];
 
1     // 发送请求
2     [request startSynchronous]; // 同步请求
3 //    [request startAsynchronous]; // 异步请求
 
 
3.处理返回信息
(1)request属性
  • request.error
  • request.responseStatusCode
  • request.responseStatusMessage
  • request.responseData
  • request.responseString
 
发送请求后:
1     if (request.error) {
2         NSLog(@"请求出错");
3     }
 
 
(2)使用代理
遵守 <ASIHTTPRequestDelegate>
 
 1 /** get请求 */
 2 - (void)sendByGet{
 3     // 创建请求
 4     NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/login?user=tom&password=123"];
 5     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
 6   
 7     // 设置请求
 8     request.delegate = self;
 9     // 发送请求
10     [request startAsynchronous]; // 异步请求
11 }
12  
13 #pragma mark - ASIHTTPRequestDelegate
14 /** 使用代理处理请求事件 */
15 - (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data {
16     NSLog(@"正在接受数据");
17 }
18  
 
(3)使用selector(基于代理方法,会覆盖代理方法)
====>此方法也要设置代理
 1 /** get请求 */
 2 - (void)sendByGet{
 3     // 创建请求
 4     NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/login?user=tom&password=123"];
 5     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
 6   
 7     // 设置请求
 8     request.delegate = self;
 9    
10     // 使用selector处理请求事件
11     [request setDidStartSelector:@selector(startRequest)];
12     // 发送请求
13     [request startAsynchronous]; // 异步请求
14 }
15 
16 /** 请求开始 */
17 - (void) startRequest {
18     NSLog(@"请求开始");
19 }
 
(4)使用block
开启block:[request setStartedBlock:(void ^block)];
接收数据block:[request setDataReceive:(void ^block)];
结束block:[request setCompletionBlock:(void ^block)];
 1 /** get请求 */
 2 - (void)sendByGet{
 3     // 创建请求
 4     NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/login?user=tom&password=123"];
 5     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
 6    
 7     // 设置请求
 8     request.timeOutSeconds = 10; // 超时时间
 9     request.delegate = self;
10    
11     // 使用block处理请求事件
12     [request setCompletionBlock:^{
13         NSLog(@"请求完成!");
14     }];
15   
16     // 发送请求
17     [request startAsynchronous]; // 异步请求
18 }
 
 
4.其实,需要把request作为成员变量,控制器被销毁,切记要先取消、清除request
====> 否则request的response返回的时候会发生野指针错误
 1 @interface ViewController () <ASIHTTPRequestDelegate>
 2 @property(nonatomic, strong) ASIHTTPRequest *request;
 3 @end
 4  
 5 #pragma mark - dealloc
 6 /** 控制器销毁之前,一定要取消、清除成员request代理 */
 7 - (void)dealloc {
 8     [self.request clearDelegatesAndCancel];
 9     self.request = nil;
10 }
 
 
C.其他用法
1.ASIFormDataRequest继承NSOperation,可以放到queue中管理
2.网络请求状态
3.显示网络请求状态(状态栏指示动画圈)
4.支持后台网络请求
5.设置请求超时重试次数
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/hellovoidworld/p/4257690.html