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

  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请求:

    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];

AFNetWorking 队列请求的更多相关文章

  1. AFNetworking网络请求数据

    //创建AFNetworking的请求操作    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWit ...

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

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

  3. iOS - AFNetworking 网络请求

    前言 在 iOS 开发中,一般情况下,简单的向某个 Web 站点简单的页面提交请求并获取服务器的响应,用 Xcode 自带的 NSURLConnection 是能胜任的.但是,在绝大部分下我们所需要访 ...

  4. AFNetWorking https请求 SSL认证 自制证书

    1.服务器会给一个证书,一般为.pem格式证书 2.将.pem格式的证书转换成.cer格式的证书 打开电脑自带终端 ,进入到桌面  cd Desktop 回车回到桌面Desktop Admin$ 输入 ...

  5. AFNetworking网络请求的get和post步骤

      1.首先通过第三方:CocoaPods下载AFNetworking 1.1.先找到要查找的三方库:pod search + AFNetworking 1.2.出来一堆列表页面,选择三方库最新版本命 ...

  6. AFNetworking Delete请求,报参数为空的错误

    使用AFNetWorking进行网络请求的时候,AFNetWorking会默认把get head delete这三个方法的请求参数拼到了url的后面,然后造成body为空,一行代码解决: manage ...

  7. ios开发网络学习六:设置队列请求与RunLoop

    #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...

  8. AFNetWorking同步请求

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); //创建信号量 AFHTTPSessionManager *manager ...

  9. 给AFNetworking添加请求缓存功能实现在没有网络的情况下返回缓存数据

    原理:先给NSURLSession地Configuration设置一个内存和本地代理,原来的网络请求结束后会查找缓存的代理字典,并执行代理对象对应的操作方法,需要做的就是拦截错误的方法,返回缓存的数据 ...

随机推荐

  1. QWord2vec:word2vec移植版+GUI

    序 Word2Vec原生是不支持Windows的,索性就用Qt移植了一下. 大概做了下面几件事. ①替换LinuxAPI的pthread为QThread. ②取消了posix_memalign(),内 ...

  2. Android的Proxy/Delegate Application框架 (主要介绍插件化开发)

    1. 插件化的原理 是 Java ClassLoader 的原理:Java ClassLoader基础 常用的其他解决方法还包括:Google Multidex,用 H5 代替部分逻辑,删无用代码,买 ...

  3. [知识点]网络流之Dinic算法

    // 此博文为迁移而来,写于2015年2月6日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vrg4.html      ...

  4. JavaScript进阶(三)之对象

    返回星期方法 getDay() 返回星期,返回的是0-6的数字,0 表示星期天.如果要返回相对应“星期”,通过数组完成,代码如下: <script type="text/javascr ...

  5. 20145330第五周《Java学习笔记》

    20145330第五周<Java学习笔记> 这一周又是紧张的一周. 语法与继承架构 Java中所有错误都会打包为对象可以尝试try.catch代表错误的对象后做一些处理. 使用try.ca ...

  6. 自己签发免费ssl证书

    自己制作ssl证书:自己签发免费ssl证书,为nginx生成自签名ssl证书 这里说下Linux 系统怎么通过openssl命令生成 证书. 首先执行如下命令生成一个keyopenssl genrsa ...

  7. 升级WebService图形服务,将K10.2和K10.3写到一个类库,所有服务放在一个类库

    问题描述: 平时负责电子政务和图形调用部分,凡是牵涉到图形的都需要调用WebService服务,因此很多工程都需要添加web服务引用,现在WebForm的工程一个是10.2版本,一个是10.3版本,区 ...

  8. python 之ConfigParser

    ConfigParser 简介ConfigParser是用来操作配置文件的模块. 说明:[**]为配置文件的section,基本格式为 [section] key = valueeg: [db] db ...

  9. Java中的super与this解析

    好了,现在开始讨论this&super这两个关键字的意义和用法. 在Java中,this通常指当前对象,super则指父类的.当你想要引用当前对象的某种东西,比如当前对象的某个方法,或当前对象 ...

  10. [LintCode] Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number`. Ugly numbers are positive number ...