我们在开发过程中,经常会遇到有些页面不止一个网络请求,有时候需要两个三个甚至更多,这个时候我们就需要队列请求,下边是GET请求的多个请求放在队列里边:

[objc] view plaincopyprint?
  1. NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
  2. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  3. AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  4. [operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  5. NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
  6. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  7. NSLog(@"Error: %@", error);
  8. }];
  9. NSURL *url2 = [NSURL URLWithString:@"http://www.sohu.com"];
  10. NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
  11. AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request2];
  12. [operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  13. NSLog(@"Response2: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
  14. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  15. NSLog(@"Error: %@", error);
  16. }];
  17. NSURL *url3 = [NSURL URLWithString:@"http://www.sina.com"];
  18. NSURLRequest *request3 = [NSURLRequest requestWithURL:url3];
  19. AFHTTPRequestOperation *operation3 = [[AFHTTPRequestOperation alloc] initWithRequest:request3];
  20. [operation3 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  21. NSLog(@"Response3: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
  22. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  23. NSLog(@"Error: %@", error);
  24. }];
  25. //同时请求
  26. NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
  27. [operationQueue setMaxConcurrentOperationCount:3];
  28. [operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];
  29. //operation2 在 operation1 请求完成后执行
  30. NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
  31. [operation2 addDependency:operation1];
  32. [operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];

下边是POST请求:

[objc] view plaincopyprint?
  1. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://gowalla.com/friendships/request?user_id=1699"]];
  2. [request setHTTPMethod:@"POST"];
  3. NSDictionary *headers = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Token token=\"%@\"", kOAuthToken] forKey:@"Authorization"];
  4. [request setAllHTTPHeaderFields:headers];
  5. AFHTTPRequestOperation *operation = [AFHTTPRequestOperation operationWithRequest:request completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
  6. BOOL HTTPStatusCodeIsAcceptable = [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)] containsIndex:[response statusCode]];
  7. if (HTTPStatusCodeIsAcceptable) {
  8. NSLog(@"Friend Request Sent");
  9. } else {
  10. NSLog(@"[Error]: (%@ %@) %@", [request HTTPMethod], [[request URL] relativePath], error);
  11. }
  12. }];
  13. NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
  14. [queue addOperation:operation];

转载于:https://www.cnblogs.com/weiboyuan/p/5053246.html

AFNetWorking 队列请求相关推荐

  1. AFNetworking post请求遇到问题解决过程

    AFNetworking post请求遇到问题解决过程 参考文章: (1)AFNetworking post请求遇到问题解决过程 (2)https://www.cnblogs.com/yuancong ...

  2. 设置AFNetworking网络请求的超时时间

    设置AFNetworking网络请求的超时时间 也许大家使用的时候已经察觉到,设置AFNetworking的超时时间并不管用,但可以用特殊的方式来处理. 以下是笔者基于AFNetworking2.5. ...

  3. AFNetworking网络请求与图片上传工具(POST)

    AFNetworking网络请求与图片上传工具(POST) .h文件 #import <Foundation/Foundation.h>/** 成功Block */ typedef voi ...

  4. AFNetworking网络请求失败

    之前iOS开发一直用ASIHTTPRequest进行网络请求,但是这个框架很长时间已经不更新了.我了解到目前很多应用都在使用AFNetworking,今天尝试了一下,发现了不少问题. 目前已经是AFN ...

  5. AFNetworking网络请求详解

    一.AFURLSessionManager 网络请求的核心类,主要作用有以下几点: 1.线程安全的创建NSURLSession,并实现NSURLSessionDelegate 2.设置response ...

  6. AFNetworking get请求与 post 请求操作

    get 请求方式 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",kBYBaseURL]];N ...

  7. AFNetworking 取消请求

    取消单个操作: AFHTTPRequestOperation *operation= [[AFHTTPRequestOperation alloc] initWithRequest:request]; ...

  8. AFNetWorking网络请求

    NetWorkAPIClient.h #import <Foundation/Foundation.h> #import "AFHTTPRequestOperationManag ...

  9. AFNetworking为什会请求不到数据

    最近在做一个项目时,在网络请求的时遇到一个很惆怅的问题, 问题的大概就是,我用AFNetworking发出一个请求,但是返回来的数据是为空,而当时安卓这边用同样的接口,发出请求同样的接口,有请求到了数 ...

最新文章

  1. word受权限保护无法打开_双击文档无法打开到底是啥毛病?简单一招解决
  2. Ubuntu16.04 + ROS kinetic + 激光slam-cartographer ROS + 数据仿真 + Turtlebot3仿真
  3. 使用PIE/PIF值判断DVD刻录机的刻录品质
  4. 【Spring 基础篇三】属性注入与属性编辑器
  5. python后端技术栈_Python后端技术栈(三)--设计模式
  6. Ubuntu boost库文件安装编译
  7. AD19 add pins to nets错误_《英雄联盟手游》错误代码问题大全 LOL的错误代码都是什么意思...
  8. 【LeetCode刷题】23. 合并K个排序链表
  9. python编辑器文字放大_python学习笔记000
  10. Android 学习路线
  11. IMX8基于FFT的GPU和CPU的性能测试
  12. libcurl基础教程及封装(支持同步异步请求,支持多线程下载,支持https)
  13. excel比对两列数据
  14. Linux系统搭建房卡游戏教程,2020年H5电玩房卡游戏源码合集运营级:超强后台控制+部署教程文档...
  15. Python 之 pygame 学习(入门知识)
  16. Mvtec数据集批量加各种噪声(高斯,椒盐,随机噪声)
  17. sd3403开发板学习(一)
  18. Error —— 单元测试中如何Mock application文件中的数据?
  19. 字符串处理:输入字符串s1和s2以及插入位置f,在字符串s1中的指定位置f处插入字符串s2。如输入BEIJING, 123, 3,则输出:BEI123JING。
  20. 127Echarts - 关系图(Graph Life Expectancy)

热门文章

  1. 2022-2028年中国抗菌玻璃行业市场研究及前瞻分析报告
  2. 2022-2028年现代农业背景下中国家庭农场深度调研及投资前景预测报告
  3. Go 知识点(01)— 主协程与子协程执行顺序
  4. 力所能及的做些自己喜欢和有趣的事
  5. MindSpore整体架构介绍
  6. 驾驶员行为监控系统:需要它来管理车队
  7. 汽车HUD(Head-up Display)的技术难点
  8. Libzip 版本过低或提示reinstall解决方法
  9. Android 一个activity 加载一个Fragment
  10. Could not get unknown property ‘compileSdkVersion‘ for project