日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

AFNetWorking 队列请求

發布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AFNetWorking 队列请求 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我們在開發過程中,經常會遇到有些頁面不止一個網絡請求,有時候需要兩個三個甚至更多,這個時候我們就需要隊列請求,下邊是GET請求的多個請求放在隊列里邊:

?

[objc]?view plaincopyprint?
  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請求:

?

[objc]?view plaincopyprint?
    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]; ?

轉載于:https://www.cnblogs.com/weiboyuan/p/5053246.html

總結

以上是生活随笔為你收集整理的AFNetWorking 队列请求的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。