[iOS 多线程 & 网络 - 2.9] - ASI框架

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