AFNetWorking 队列请求
我们在开发过程中,经常会遇到有些页面不止一个网络请求,有时候需要两个三个甚至更多,这个时候我们就需要队列请求,下边是GET请求的多个请求放在队列里边:
- 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请求:
- 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];
AFNetWorking 队列请求的更多相关文章
- AFNetworking网络请求数据
//创建AFNetworking的请求操作 AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWit ...
- AFNetworking网络请求与图片上传工具(POST)
AFNetworking网络请求与图片上传工具(POST) .h文件 #import <Foundation/Foundation.h> /** 成功Block */ typedef vo ...
- iOS - AFNetworking 网络请求
前言 在 iOS 开发中,一般情况下,简单的向某个 Web 站点简单的页面提交请求并获取服务器的响应,用 Xcode 自带的 NSURLConnection 是能胜任的.但是,在绝大部分下我们所需要访 ...
- AFNetWorking https请求 SSL认证 自制证书
1.服务器会给一个证书,一般为.pem格式证书 2.将.pem格式的证书转换成.cer格式的证书 打开电脑自带终端 ,进入到桌面 cd Desktop 回车回到桌面Desktop Admin$ 输入 ...
- AFNetworking网络请求的get和post步骤
1.首先通过第三方:CocoaPods下载AFNetworking 1.1.先找到要查找的三方库:pod search + AFNetworking 1.2.出来一堆列表页面,选择三方库最新版本命 ...
- AFNetworking Delete请求,报参数为空的错误
使用AFNetWorking进行网络请求的时候,AFNetWorking会默认把get head delete这三个方法的请求参数拼到了url的后面,然后造成body为空,一行代码解决: manage ...
- ios开发网络学习六:设置队列请求与RunLoop
#import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...
- AFNetWorking同步请求
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); //创建信号量 AFHTTPSessionManager *manager ...
- 给AFNetworking添加请求缓存功能实现在没有网络的情况下返回缓存数据
原理:先给NSURLSession地Configuration设置一个内存和本地代理,原来的网络请求结束后会查找缓存的代理字典,并执行代理对象对应的操作方法,需要做的就是拦截错误的方法,返回缓存的数据 ...
随机推荐
- input属性控制弹出键盘类型
/** * ios弹起数字键盘有三种方法 * 1. <input type="number"> 可以弹起带有小数点的键盘,可以键盘不干净,有其它各种字符,可切换 ...
- 向 ViewPager 中添加 包含 ListView 的 Fragment
对与fragment就不说什么了,直接看API手册吧,亲. 向 ViewPager 中添加 包含 ListView 的 Fragment 的过程比较麻烦.他所表现的效果就是新闻客户端的滑动翻页效果. ...
- Webpack打包进阶
说在前面 由于使用了React直出,页面各项性能指标使人悦目.本篇将深入探讨目前PC部落所采用webpack打包优化策略,以及探讨PC部落并未使用的 webpack Code Splitting 代码 ...
- phpunit测试成功 phpunit测试实践代码
16:12 2015/12/8phpunit测试成功,代码写在www目录下,以类名命名代码文件,我的文件名为 ArrayTest.php,类名为ArrayTest,内部写了简单的测试代码:<?p ...
- Codeforces Beta Round #2
A题,神题意题.. #include <iostream> #include <cstdio> #include <cstring> #include <st ...
- db2工具优化
- [转载]触发ASSERT(afxCurrentResourceHandle != NULL)错误的原因
触发ASSERT(afxCurrentResourceHandle != NULL)错误的原因 Debug Assert error afxwin1.inl line:22 翻译参考 http://w ...
- ArcGIS AddIN之工具不可用
AddIN做的工具,第一次打开时工具亮起(可用),第一次点击之后工具就灰掉(不可用) 原因:该工具没有找到响应函数 具体原因:之前做的好多工具,好多个项目,好多代码.现在统一放到一个项目中,一个工具条 ...
- 定位position
position : absolute | relative | fixed | static 定位:子集可以超出父级的范围,如父级蛇overflow:hidden则看不到. absolute : 绝 ...
- svn版本升级遇到的bug
从svn1.0升级到svn10.x时发生的bug 1.可能是没有javahl这个架包或是与你升级后的svn版本不匹配,eclipse在线下载就好 2.项目因为之前连接是较低版本的svn,当svn升级后 ...