ASIHTTPRequest框架使用(2)--发送异步请求
2014-05-09 13:37
627 查看
同步请求一般只是用在某个子线程中使用,而不在主线程中使用。异步请求的用户体验要比同步请求好,因此一般情况下异步请求用的多。
ASIHTTPRequest和ASIFormDataRequest两个请求类都可以发送异步请求。ASIFormDataRequest继承了ASIHTTPRequest异步请求方法,所以重点介绍ASIHTTPRequest的异步请求。
1、借助Delegate处理请求
- (void)startRequest
{
NSString *strUrl = [[NSString alloc] initWithFormat:@"http://iosbook3.com/service/mynotes/webservice.php? email=%@&type=%@&action=%@",@"ios_yaoxinchao@163.com",@"JSON",@"query"];
NSURL *url = [NSURL URLWithString:[strUrl URLEncodedString]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// 设置代理
[request setDelegate:self];
[request startAsynchronous];
}
#pragma mark - 代理方法 【默认调用的代理方法】
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *data = [request responseData];
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
。。。。
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"%@",[error localizedDescription]);
}
自定义回调方法:
- (void)startRequest
{
....
// 自定义回调方法
[request setDidFinishSelector:@selector(requestSuccess:)];
[request setDidFailSelector:@selector(requestError:)];
}
- (void)requestSuccess:(ASIHTTPRequest *)request
{
NSData *data = [request responseData];
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
。。。。
}
- (void)requestError:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"%@",[error localizedDescription]);
}
2、借助“Block”处理请求
- (void)startRequest
{
NSString *strUrl = [[NSString alloc] initWithFormat:@"http://iosbook3.com/service/mynotes/webservice.php? email=%@&type=%@&action=%@",@"ios_yaoxinchao@163.com",@"JSON",@"query"];
NSURL *url = [NSURL URLWithString:[strUrl URLEncodedString]];
__weak ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 【1】
[request setCompletionBlock:^{ // 设置成功完成时回调的Block
NSData *data = [request responseData];
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
。。。。
}];
[request setFailedBlock:^{ // 设置失败时回调的Block
NSError *error = [request error];
NSLog(@"%@",[error localizedDescription]);
}];
[request startAsynchronous];
}
【1】:__weak关键字的意思是ASIHTTPRequest对象是弱引用,不进行保持处理。目的是为了避免循环引用,造成内存泄露。
ASIHTTPRequest和ASIFormDataRequest两个请求类都可以发送异步请求。ASIFormDataRequest继承了ASIHTTPRequest异步请求方法,所以重点介绍ASIHTTPRequest的异步请求。
1、借助Delegate处理请求
- (void)startRequest
{
NSString *strUrl = [[NSString alloc] initWithFormat:@"http://iosbook3.com/service/mynotes/webservice.php? email=%@&type=%@&action=%@",@"ios_yaoxinchao@163.com",@"JSON",@"query"];
NSURL *url = [NSURL URLWithString:[strUrl URLEncodedString]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// 设置代理
[request setDelegate:self];
[request startAsynchronous];
}
#pragma mark - 代理方法 【默认调用的代理方法】
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *data = [request responseData];
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
。。。。
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"%@",[error localizedDescription]);
}
自定义回调方法:
- (void)startRequest
{
....
// 自定义回调方法
[request setDidFinishSelector:@selector(requestSuccess:)];
[request setDidFailSelector:@selector(requestError:)];
}
- (void)requestSuccess:(ASIHTTPRequest *)request
{
NSData *data = [request responseData];
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
。。。。
}
- (void)requestError:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"%@",[error localizedDescription]);
}
2、借助“Block”处理请求
- (void)startRequest
{
NSString *strUrl = [[NSString alloc] initWithFormat:@"http://iosbook3.com/service/mynotes/webservice.php? email=%@&type=%@&action=%@",@"ios_yaoxinchao@163.com",@"JSON",@"query"];
NSURL *url = [NSURL URLWithString:[strUrl URLEncodedString]];
__weak ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 【1】
[request setCompletionBlock:^{ // 设置成功完成时回调的Block
NSData *data = [request responseData];
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
。。。。
}];
[request setFailedBlock:^{ // 设置失败时回调的Block
NSError *error = [request error];
NSLog(@"%@",[error localizedDescription]);
}];
[request startAsynchronous];
}
【1】:__weak关键字的意思是ASIHTTPRequest对象是弱引用,不进行保持处理。目的是为了避免循环引用,造成内存泄露。
相关文章推荐
- tcp_wrappers 学习
- http://android.yaohuiji.com/archives/935
- 使用eclipse的TCP/IP Monitor监控Web service程序
- TCP/IP、Http的区别
- TCP/IP、Http的区别
- Tomcat 7 HTTP 连接器
- 服务器端环境由Windows移至Unix时出的问题
- 使用universal-image-loader-1.8.6-with-sources加载网络图片
- linux lvs持久链接和防火墙标记实现http和https共享同一集群服务
- 网络协议模拟之QQ微博分享接口应用
- 从输入 URL 到页面加载完的过程中都发生了什么事情 —— 网络优化篇
- [python网络编程]DNS服务器
- CHM文档编辑制作及电子书的相关问题 - txfast的日志 - 网易博客 http://txfast.blog.163.com/blog/static/40536042200832065417/
- 网络营销师职业前景如何?
- Eclipse Memory Analyzer 安装(Update Site: http://download.eclipse.org/mat/1.3.1/update-site/ )
- CentOS查看CPU、内存、网络流量和磁盘 I/O
- 软考之路(1)——浅解网络基础知识
- ASIHttpRequest编译不通过
- ASIHttpRequest编译不通过
- 计算机网络-自顶向下方法和Internet特色笔记之三(中)