iOS---iOS开发零基础教程之AFNetWorking POST 队列请求
2015-11-23 20:33
453 查看
转载自:http://blog.csdn.net/crazyzhang1990/article/details/46430321
我们在开发过程中,经常会遇到有些页面不止一个网络请求,有时候需要两个三个甚至更多,这个时候我们就需要队列请求,下边是GET请求的多个请求放在队列里边:
[objc] view
plaincopyprint?
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
NSURL *url2 = [NSURL URLWithString:@"http://www.sohu.com"];
NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request2];
[operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response2: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
NSURL *url3 = [NSURL URLWithString:@"http://www.sina.com"];
NSURLRequest *request3 = [NSURLRequest requestWithURL:url3];
AFHTTPRequestOperation *operation3 = [[AFHTTPRequestOperation alloc] initWithRequest:request3];
[operation3 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response3: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
//同时请求
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue setMaxConcurrentOperationCount:3];
[operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];
//operation2 在 operation1 请求完成后执行
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operation2 addDependency:operation1];
[operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];
下边是POST请求:
[objc] view
plaincopyprint?
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://gowalla.com/friendships/request?user_id=1699"]];
[request setHTTPMethod:@"POST"];
NSDictionary *headers = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Token token=\"%@\"", kOAuthToken] forKey:@"Authorization"];
[request setAllHTTPHeaderFields:headers];
AFHTTPRequestOperation *operation = [AFHTTPRequestOperation operationWithRequest:request completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
BOOL HTTPStatusCodeIsAcceptable = [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)] containsIndex:[response statusCode]];
if (HTTPStatusCodeIsAcceptable) {
NSLog(@"Friend Request Sent");
} else {
NSLog(@"[Error]: (%@ %@) %@", [request HTTPMethod], [[request URL] relativePath], error);
}
}];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
参考文献:http://cocoadocs.org/docsets/AFNetworking/0.5.1/
我们在开发过程中,经常会遇到有些页面不止一个网络请求,有时候需要两个三个甚至更多,这个时候我们就需要队列请求,下边是GET请求的多个请求放在队列里边:
[objc] view
plaincopyprint?
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
NSURL *url2 = [NSURL URLWithString:@"http://www.sohu.com"];
NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request2];
[operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response2: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
NSURL *url3 = [NSURL URLWithString:@"http://www.sina.com"];
NSURLRequest *request3 = [NSURLRequest requestWithURL:url3];
AFHTTPRequestOperation *operation3 = [[AFHTTPRequestOperation alloc] initWithRequest:request3];
[operation3 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response3: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
//同时请求
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue setMaxConcurrentOperationCount:3];
[operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];
//operation2 在 operation1 请求完成后执行
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operation2 addDependency:operation1];
[operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];
下边是POST请求:
[objc] view
plaincopyprint?
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://gowalla.com/friendships/request?user_id=1699"]];
[request setHTTPMethod:@"POST"];
NSDictionary *headers = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Token token=\"%@\"", kOAuthToken] forKey:@"Authorization"];
[request setAllHTTPHeaderFields:headers];
AFHTTPRequestOperation *operation = [AFHTTPRequestOperation operationWithRequest:request completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
BOOL HTTPStatusCodeIsAcceptable = [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)] containsIndex:[response statusCode]];
if (HTTPStatusCodeIsAcceptable) {
NSLog(@"Friend Request Sent");
} else {
NSLog(@"[Error]: (%@ %@) %@", [request HTTPMethod], [[request URL] relativePath], error);
}
}];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
参考文献:http://cocoadocs.org/docsets/AFNetworking/0.5.1/
相关文章推荐
- IOS 证书设置指南
- iOS--同时请求的问题(用不到)
- iOS开发:获取文件路径总结
- iOS--通过URL获取图片
- ios autolayout
- iOS编程学习过程中常见bug记录-持续记录
- IOS 如何在两个控制器之间传值-使用delegate
- iOS攻略之收到内存警告的处理(转)
- iOS中的CoreData的使用(详解)
- iOS企业版分发关于plist和证书的那些事
- iOS开发之回调delegate的方法时判断delegate是否已经被释放
- iOS8系统H264视频硬件编解码说明
- iOS字符串个别字符特殊颜色
- 解决iOS9(Xcode7)中动态库.tbd 报错问题
- 【iOS】OC-AFNetworking 2.0 跟踪文件上传进度
- 【iOS】OC-AFNetworking 2.0 跟踪文件上传进度
- 【整理】FFMPEG相关开源项目
- iOS 取相册照片/打开相机
- iOS解决键盘遮挡问题
- iOS开发中MD5加密方式