码迷,mamicode.com
首页 > Web开发 > 详细

AFNetWorking 队列请求

时间:2015-12-17 12:15:43      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

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

 

  1. NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];  
  2.   
  3.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  4.   
  5.     AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request];  
  6.   
  7.     [operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {  
  8.   
  9.         NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);  
  10.   
  11.     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  12.   
  13.         NSLog(@"Error: %@", error);  
  14.   
  15.     }];  
  16.   
  17.       
  18.   
  19.       
  20.   
  21.     NSURL *url2 = [NSURL URLWithString:@"http://www.sohu.com"];  
  22.   
  23.     NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];  
  24.   
  25.     AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request2];  
  26.   
  27.     [operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {  
  28.   
  29.         NSLog(@"Response2: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);  
  30.   
  31.     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  32.   
  33.         NSLog(@"Error: %@", error);  
  34.   
  35.     }];  
  36.   
  37.       
  38.   
  39.       
  40.   
  41.       
  42.   
  43.     NSURL *url3 = [NSURL URLWithString:@"http://www.sina.com"];  
  44.   
  45.     NSURLRequest *request3 = [NSURLRequest requestWithURL:url3];  
  46.   
  47.     AFHTTPRequestOperation *operation3 = [[AFHTTPRequestOperation alloc] initWithRequest:request3];  
  48.   
  49.     [operation3 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {  
  50.   
  51.         NSLog(@"Response3: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);  
  52.   
  53.     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  54.   
  55.         NSLog(@"Error: %@", error);  
  56.   
  57.     }];  
  58.   
  59.       
  60.   
  61.       
  62.   
  63.     //同时请求  
  64.   
  65.     NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];  
  66.   
  67.     [operationQueue setMaxConcurrentOperationCount:3];  
  68.   
  69.     [operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];  
  70.   
  71.    
  72.   
  73.       
  74.   
  75.     //operation2 在 operation1 请求完成后执行  
  76.   
  77.     NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];  
  78.   
  79.     [operation2 addDependency:operation1];  
  80.   
  81.     [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.   
    4. NSDictionary *headers = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Token token=\"%@\"", kOAuthToken] forKey:@"Authorization"];  
    5. [request setAllHTTPHeaderFields:headers];  
    6.   
    7. AFHTTPRequestOperation *operation = [AFHTTPRequestOperation operationWithRequest:request completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {  
    8.     BOOL HTTPStatusCodeIsAcceptable = [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)] containsIndex:[response statusCode]];  
    9.     if (HTTPStatusCodeIsAcceptable) {  
    10.         NSLog(@"Friend Request Sent");  
    11.     } else {  
    12.         NSLog(@"[Error]: (%@ %@) %@", [request HTTPMethod], [[request URL] relativePath], error);  
    13.     }  
    14. }];  
    15.   
    16. NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];  
    17. [queue addOperation:operation];  

AFNetWorking 队列请求

标签:

原文地址:http://www.cnblogs.com/weiboyuan/p/5053246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!