AFNetWorking 队列请求
生活随笔
收集整理的這篇文章主要介紹了
AFNetWorking 队列请求
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們在開發過程中,經常會遇到有些頁面不止一個網絡請求,有時候需要兩個三個甚至更多,這個時候我們就需要隊列請求,下邊是GET請求的多個請求放在隊列里邊:
?
[objc]?view plaincopyprint?- NSURL?*url?=?[NSURL?URLWithString:@"http://www.baidu.com"];??
- ??
- ????NSURLRequest?*request?=?[NSURLRequest?requestWithURL:url];??
- ??
- ????AFHTTPRequestOperation?*operation1?=?[[AFHTTPRequestOperation?alloc]?initWithRequest:request];??
- ??
- ????[operation1?setCompletionBlockWithSuccess:^(AFHTTPRequestOperation?*operation,?id?responseObject)?{??
- ??
- ????????NSLog(@"Response:?%@",?[[NSString?alloc]?initWithData:responseObject?encoding:NSUTF8StringEncoding]);??
- ??
- ????}?failure:^(AFHTTPRequestOperation?*operation,?NSError?*error)?{??
- ??
- ????????NSLog(@"Error:?%@",?error);??
- ??
- ????}];??
- ??
- ??????
- ??
- ??????
- ??
- ????NSURL?*url2?=?[NSURL?URLWithString:@"http://www.sohu.com"];??
- ??
- ????NSURLRequest?*request2?=?[NSURLRequest?requestWithURL:url2];??
- ??
- ????AFHTTPRequestOperation?*operation2?=?[[AFHTTPRequestOperation?alloc]?initWithRequest:request2];??
- ??
- ????[operation2?setCompletionBlockWithSuccess:^(AFHTTPRequestOperation?*operation,?id?responseObject)?{??
- ??
- ????????NSLog(@"Response2:?%@",?[[NSString?alloc]?initWithData:responseObject?encoding:NSUTF8StringEncoding]);??
- ??
- ????}?failure:^(AFHTTPRequestOperation?*operation,?NSError?*error)?{??
- ??
- ????????NSLog(@"Error:?%@",?error);??
- ??
- ????}];??
- ??
- ??????
- ??
- ??????
- ??
- ??????
- ??
- ????NSURL?*url3?=?[NSURL?URLWithString:@"http://www.sina.com"];??
- ??
- ????NSURLRequest?*request3?=?[NSURLRequest?requestWithURL:url3];??
- ??
- ????AFHTTPRequestOperation?*operation3?=?[[AFHTTPRequestOperation?alloc]?initWithRequest:request3];??
- ??
- ????[operation3?setCompletionBlockWithSuccess:^(AFHTTPRequestOperation?*operation,?id?responseObject)?{??
- ??
- ????????NSLog(@"Response3:?%@",?[[NSString?alloc]?initWithData:responseObject?encoding:NSUTF8StringEncoding]);??
- ??
- ????}?failure:^(AFHTTPRequestOperation?*operation,?NSError?*error)?{??
- ??
- ????????NSLog(@"Error:?%@",?error);??
- ??
- ????}];??
- ??
- ??????
- ??
- ??????
- ??
- ????//同時請求??
- ??
- ????NSOperationQueue?*operationQueue?=?[[NSOperationQueue?alloc]?init];??
- ??
- ????[operationQueue?setMaxConcurrentOperationCount:3];??
- ??
- ????[operationQueue?addOperations:@[operation1,?operation2,?operation3]?waitUntilFinished:NO];??
- ??
- ???
- ??
- ??????
- ??
- ????//operation2?在?operation1?請求完成后執行??
- ??
- ????NSOperationQueue?*operationQueue?=?[[NSOperationQueue?alloc]?init];??
- ??
- ????[operation2?addDependency:operation1];??
- ??
- ????[operationQueue?addOperations:@[operation1,?operation2,?operation3]?waitUntilFinished:NO];??
?
?
下邊是POST請求:
?
[objc]?view plaincopyprint?- NSMutableURLRequest?*request?=?[NSMutableURLRequest?requestWithURL:[NSURL?URLWithString:@"https://gowalla.com/friendships/request?user_id=1699"]];??
- [request?setHTTPMethod:@"POST"];??
- ??
- NSDictionary?*headers?=?[NSDictionary?dictionaryWithObject:[NSString?stringWithFormat:@"Token?token=\"%@\"",?kOAuthToken]?forKey:@"Authorization"];??
- [request?setAllHTTPHeaderFields:headers];??
- ??
- AFHTTPRequestOperation?*operation?=?[AFHTTPRequestOperation?operationWithRequest:request?completion:^(NSURLRequest?*request,?NSHTTPURLResponse?*response,?NSData?*data,?NSError?*error)?{??
- ????BOOL?HTTPStatusCodeIsAcceptable?=?[[NSIndexSet?indexSetWithIndexesInRange:NSMakeRange(200,?100)]?containsIndex:[response?statusCode]];??
- ????if?(HTTPStatusCodeIsAcceptable)?{??
- ????????NSLog(@"Friend?Request?Sent");??
- ????}?else?{??
- ????????NSLog(@"[Error]:?(%@?%@)?%@",?[request?HTTPMethod],?[[request?URL]?relativePath],?error);??
- ????}??
- }];??
- ??
- NSOperationQueue?*queue?=?[[[NSOperationQueue?alloc]?init]?autorelease];??
- [queue?addOperation:operation]; ?
轉載于:https://www.cnblogs.com/weiboyuan/p/5053246.html
總結
以上是生活随笔為你收集整理的AFNetWorking 队列请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 家用电多少钱一度啊?
- 下一篇: 患输卵管积水的前兆