zoukankan      html  css  js  c++  java
  • AFNetWorking 队列请求

    我们在开发过程中,经常会遇到有些页面不止一个网络请求,有时候需要两个三个甚至更多,这个时候我们就需要队列请求,下边是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];  
  • 相关阅读:
    call()与apply()的作用与区别
    Tomcat8/9的catalina.out中文乱码问题解决
    怎样查看Jenkins的版本
    每日日报2020.8.18
    528. Random Pick with Weight
    875. Koko Eating Bananas
    721. Accounts Merge
    515. Find Largest Value in Each Tree Row
    286. Walls and Gates (Solution 1)
    408. Valid Word Abbreviation
  • 原文地址:https://www.cnblogs.com/weiboyuan/p/5053246.html
Copyright © 2011-2022 走看看