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];
转载于:https://www.cnblogs.com/weiboyuan/p/5053246.html
AFNetWorking 队列请求相关推荐
- AFNetworking post请求遇到问题解决过程
AFNetworking post请求遇到问题解决过程 参考文章: (1)AFNetworking post请求遇到问题解决过程 (2)https://www.cnblogs.com/yuancong ...
- 设置AFNetworking网络请求的超时时间
设置AFNetworking网络请求的超时时间 也许大家使用的时候已经察觉到,设置AFNetworking的超时时间并不管用,但可以用特殊的方式来处理. 以下是笔者基于AFNetworking2.5. ...
- AFNetworking网络请求与图片上传工具(POST)
AFNetworking网络请求与图片上传工具(POST) .h文件 #import <Foundation/Foundation.h>/** 成功Block */ typedef voi ...
- AFNetworking网络请求失败
之前iOS开发一直用ASIHTTPRequest进行网络请求,但是这个框架很长时间已经不更新了.我了解到目前很多应用都在使用AFNetworking,今天尝试了一下,发现了不少问题. 目前已经是AFN ...
- AFNetworking网络请求详解
一.AFURLSessionManager 网络请求的核心类,主要作用有以下几点: 1.线程安全的创建NSURLSession,并实现NSURLSessionDelegate 2.设置response ...
- AFNetworking get请求与 post 请求操作
get 请求方式 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",kBYBaseURL]];N ...
- AFNetworking 取消请求
取消单个操作: AFHTTPRequestOperation *operation= [[AFHTTPRequestOperation alloc] initWithRequest:request]; ...
- AFNetWorking网络请求
NetWorkAPIClient.h #import <Foundation/Foundation.h> #import "AFHTTPRequestOperationManag ...
- AFNetworking为什会请求不到数据
最近在做一个项目时,在网络请求的时遇到一个很惆怅的问题, 问题的大概就是,我用AFNetworking发出一个请求,但是返回来的数据是为空,而当时安卓这边用同样的接口,发出请求同样的接口,有请求到了数 ...
最新文章
- word受权限保护无法打开_双击文档无法打开到底是啥毛病?简单一招解决
- Ubuntu16.04 + ROS kinetic + 激光slam-cartographer ROS + 数据仿真 + Turtlebot3仿真
- 使用PIE/PIF值判断DVD刻录机的刻录品质
- 【Spring 基础篇三】属性注入与属性编辑器
- python后端技术栈_Python后端技术栈(三)--设计模式
- Ubuntu boost库文件安装编译
- AD19 add pins to nets错误_《英雄联盟手游》错误代码问题大全 LOL的错误代码都是什么意思...
- 【LeetCode刷题】23. 合并K个排序链表
- python编辑器文字放大_python学习笔记000
- Android 学习路线
- IMX8基于FFT的GPU和CPU的性能测试
- libcurl基础教程及封装(支持同步异步请求,支持多线程下载,支持https)
- excel比对两列数据
- Linux系统搭建房卡游戏教程,2020年H5电玩房卡游戏源码合集运营级:超强后台控制+部署教程文档...
- Python 之 pygame 学习(入门知识)
- Mvtec数据集批量加各种噪声(高斯,椒盐,随机噪声)
- sd3403开发板学习(一)
- Error —— 单元测试中如何Mock application文件中的数据?
- 字符串处理:输入字符串s1和s2以及插入位置f,在字符串s1中的指定位置f处插入字符串s2。如输入BEIJING, 123, 3,则输出:BEI123JING。
- 127Echarts - 关系图(Graph Life Expectancy)
热门文章
- 2022-2028年中国抗菌玻璃行业市场研究及前瞻分析报告
- 2022-2028年现代农业背景下中国家庭农场深度调研及投资前景预测报告
- Go 知识点(01)— 主协程与子协程执行顺序
- 力所能及的做些自己喜欢和有趣的事
- MindSpore整体架构介绍
- 驾驶员行为监控系统:需要它来管理车队
- 汽车HUD(Head-up Display)的技术难点
- Libzip 版本过低或提示reinstall解决方法
- Android 一个activity 加载一个Fragment
- Could not get unknown property ‘compileSdkVersion‘ for project