ios - 对AFNetWorking库的进一步封装
本文提供的是对AFNetWorking库的进一步封装,包括一个处理请求的HCHttpManager,它继承于AFHTTPClient;
1 #import "AFHTTPClient.h" 2 #import "HCHttpCmd.h" 3 4 @interface HCHttpManager : AFHTTPClient 5 { 6 NSMutableArray *_cmds; 7 } 8 9 @property (nonatomic, retain) NSMutableArray *cmds; 10 11 + (HCHttpManager *)sharedInstance; 12 13 - (BOOL)processCommand:(HCHttpCmd *)cmd; 14 @end
它有一个单例方法sharedInstance,和一个添加请求到队列的方法processCommand:;
实现类如下:
1 #import "HCHttpManager.h" 2 #import "AFJSONRequestOperation.h" 3 4 NSString * const kHCBaseURLString = @"http://www.baidu.com"; // 服务器base url 5 6 @implementation HCHttpManager 7 8 + (HCHttpManager *)sharedInstance 9 { 10 static HCHttpManager *_sharedInstance = nil; 11 static dispatch_once_t oncePredicate; 12 dispatch_once(&oncePredicate, ^{ 13 _sharedInstance = [[self alloc] init]; 14 }); 15 16 return _sharedInstance; 17 } 18 19 -(id)init 20 { 21 self = [super initWithBaseURL:[NSURL URLWithString:kHCBaseURLString]]; 22 if (!self) 23 { 24 return nil; 25 } 26 27 self.cmds = [NSMutableArray array]; 28 [self registerHTTPOperationClass:[AFJSONRequestOperation class]]; 29 [self setDefaultHeader:@"Accept" value:@"application/json"]; 30 31 return self; 32 } 33 34 - (void)enqueueCmd:(HCHttpCmd *)cmd 35 { 36 [self.cmds addObject:cmd]; 37 } 38 39 - (void)dequeueCmd:(HCHttpCmd *)cmd 40 { 41 [self performSelector:@selector(delayDequeue:) withObject:cmd afterDelay:1]; 42 } 43 44 - (void)delayDequeue:(HCHttpCmd *)cmd 45 { 46 47 [self.cmds removeObject:cmd]; 48 } 49 50 - (void)enqueueHTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest 51 cmd:(HCHttpCmd*)cmd 52 view:(UIView *)view 53 success:(void (^)(id object))success 54 failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure 55 { 56 void (^_success)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) = ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 57 58 id object = JSON; 59 60 if(success) 61 success(object); 62 }; 63 void (^_failure)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) = ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 64 if(failure) 65 failure(response, error); 66 }; 67 68 AFJSONRequestOperation *operation = nil; 69 operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:urlRequest 70 success:_success 71 failure:_failure]; 72 [self enqueueHTTPRequestOperation:operation]; 73 } 74 75 - (void)requestWithCmd:(HCHttpCmd *)cmd 76 success:(void (^)(id object))success 77 failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure 78 { 79 NSMutableURLRequest *request = [self requestWithMethod:cmd.method path:cmd.path parameters:cmd.queries]; 80 81 if(cmd.headers) 82 { 83 NSArray *keys = cmd.headers.allKeys; 84 for(NSString *key in keys) 85 { 86 [request addValue:[cmd.headers objectForKey:key] forHTTPHeaderField:key]; 87 } 88 } 89 90 NSData *data = cmd.data; 91 [request setHTTPBody:data]; 92 93 [self enqueueHTTPRequestOperationWithRequest:request cmd:cmd view:nil success:success failure:failure]; 94 } 95 96 - (NSMutableURLRequest *)requestWithMethod:(NSString *)method 97 path:(NSString *)path 98 parameters:(NSDictionary *)parameters 99 { 100 if ([method isEqualToString:@"POST"]) 101 self.parameterEncoding = AFFormURLParameterEncoding; 102 else 103 self.parameterEncoding = AFJSONParameterEncoding; 104 105 NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters]; 106 107 return request; 108 } 109 110 - (BOOL)processCommand:(HCHttpCmd *)cmd 111 { 112 if(!cmd) 113 return NO; 114 115 [self enqueueCmd:cmd]; 116 117 [self requestWithCmd:cmd success:^(id object) { 118 [cmd didSuccess:object]; 119 } failure:^(NSHTTPURLResponse *response, NSError *error) { 120 [cmd performSelectorOnMainThread:@selector(didFailed:) withObject:response waitUntilDone:YES]; 121 122 [self performSelectorOnMainThread:@selector(dequeueCmd:) withObject:cmd waitUntilDone:NO]; 123 }]; 124 125 return YES; 126 } 127 128 @end
然后是对请求的封装类HCHttpCmd,.h如下
1 #import <Foundation/Foundation.h> 2 #import "AFNetworking.h" 3 4 #import "HCUtil.h" 5 #import "HCUserManager.h" 6 7 typedef void (^HCHttpCmdSuccess)(id object); 8 typedef void (^HCHttpCmdFailed)(AFHTTPRequestOperation *response); 9 10 @interface HCHttpCmd : NSObject 11 12 @property (nonatomic,copy) HCHttpCmdSuccess success; 13 @property (nonatomic,copy) HCHttpCmdFailed fail; 14 15 + (id)cmd; 16 17 -(NSString *)method; // 请求方式,"GET" or "POST" 18 -(NSString *)path; // 请求对应接口的文件在服务器上的位置,如"www.baidu.com/index"中的"index" 19 - (NSDictionary *)headers; // 请求的header 20 - (NSDictionary *)queries; // "GET"请求的参数,如"www.baidu.com/index?var=123"中的var=123
21 - (NSData *)data;
22
23 - (void)didSuccess:(id)object; // 请求成功的回调
24 - (void)didFailed:(AFHTTPRequestOperation *)response;// 请求失败的回调
25 @end
HCHttpCmd类的实现如下:
1 #import "HCHttpCmd.h" 2 3 @implementation HCHttpCmd 4 5 + (id)cmd 6 { 7 return [[self alloc] init]; 8 } 9 10 - (id)init 11 { 12 self = [super init]; 13 if(self) 14 { 15 16 } 17 return self; 18 } 19 20 -(NSString *)method 21 { 22 return @"GET"; 23 } 24 25 -(NSString *)path 26 { 27 return @"index; 28 } 29 30 - (NSDictionary *)headers 31 { 32 return nil; 33 } 34 35 - (NSDictionary *)queries 36 { 37 return nil; 38 } 39 40 - (NSData *)data 41 { 42 return nil; 43 } 44 45 - (void)didSuccess:(id)object 46 { 47 if( _success) 48 { 49 _success(object); 50 } 51 } 52 53 - (void)didFailed:(AFHTTPRequestOperation *)response 54 { 55 if( _fail) 56 { 57 _fail(response); 58 } 59 } 60 @end
用法很简单,如下
1 HCHttpCmdGetChannel *cmd = [HCHttpCmdGetChannel cmd]; 2 cmd.channel_id = @"2204"; 3 [cmd setSuccess:^(id object) { 4 LOG(@"%@", object); 5 6 }]; 7 [cmd setFail:^(AFHTTPRequestOperation *response) { 8 9 }]; 10 [[HCHttpManager sharedInstance] processCommand:cmd];
这里的HCHttpCmdGetChannel是HCHttpCmd的一个子类;
首先要确保你的工程中加入了AFNetWorking库,再加入这两个类,就OK了。至此完成。