封装 ASIHTTPRequest 多请求管理类

使用方法如上图.


下面直接发代码:

头文件

[plain]  view plain copy
  1. //  
  2. //  CWLWConnectManager.h  
  3. //  LechaoDrawGuess  
  4. //  
  5. //  Created by luoge on 12-11-19.  
  6. //  Copyright (c) 2012年 watsy. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "ASIHTTPRequest.h"  
  11. #import "ASIFormDataRequest.h"  
  12.   
  13. typedef enum {  
  14.     connectType_GET = 0,  
  15.     connectType_POST  
  16. } connectType;  
  17.   
  18. @interface CWLWConnectManager : NSObject  
  19.   
  20. + (CWLWConnectManager *) sharedInstance;  
  21. + (void) releaseInstance;  
  22.   
  23. //投递方法  
  24. - (NSUInteger) conURL:(NSString *) sURL  
  25.           connectType:(connectType) cType  
  26.                params:(NSDictionary *) params  
  27.              delegate:(id) del;  
  28.   
  29. - (NSUInteger) conURL:(NSString *) sURL  
  30.           connectType:(connectType) cType  
  31.                params:(NSDictionary *) params  
  32.              delegate:(id) del  
  33.                   tag:(NSInteger) nTag;  
  34.   
  35. - (NSUInteger) conURL:(NSString *) sURL  
  36.           connectType:(connectType) cType  
  37.                params:(NSDictionary *) params  
  38.              delegate:(id) del  
  39.                   tag:(NSInteger) nTag  
  40.                   key:(NSString *) sKey;  
  41.   
  42.   
  43. #if NS_BLOCKS_AVAILABLE  
  44. - (NSUInteger) conURL:(NSString *) sURL  
  45.           connectType:(connectType) cType  
  46.                params:(NSDictionary *) params  
  47.                result:(void(^)(BOOL bSuccess,id returnData,NSError *error)) block;  
  48.   
  49. - (NSUInteger) conURL:(NSString *) sURL  
  50.           connectType:(connectType) cType  
  51.                params:(NSDictionary *) params  
  52.                   tag:(NSInteger) nTag  
  53.                result:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag)) block;  
  54.   
  55. - (NSUInteger) conURL:(NSString *) sURL  
  56.           connectType:(connectType) cType  
  57.                params:(NSDictionary *) params  
  58.                   tag:(NSInteger) nTag  
  59.                   key:(NSString *) sKey  
  60.                result:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag, NSString *skey)) block;  
  61. #endif  
  62.   
  63. //取消网络连接  
  64. - (BOOL) cancelWithHashValue:(NSUInteger) nItemHashValue;  
  65. - (BOOL) cancelURL:(NSString *) sURL  
  66.        connectType:(connectType) cType  
  67.             params:(NSDictionary *) params  
  68.           delegate:(id) del  
  69.                tag:(NSInteger) nTag  
  70.                key:(NSString *) sKey;  
  71.   
  72. @end  
  73.   
  74.   
  75. @interface NSObject(CWLCConnection)  
  76. - (void) didCWFinishSuccessedWithData:(id) data  
  77.                                   tag:(NSInteger) nTag  
  78.                                   key:(NSString *) sKey;  
  79. - (void) didCWFinishFailedWithError:(NSError *) error  
  80.                                 tag:(NSInteger) nTag  
  81.                                 key:(NSString *) sKey;  
  82. @end  

实现文件:

[plain]  view plain copy
  1. //  
  2. //  CWLWConnectManager.m  
  3. //  LechaoDrawGuess  
  4. //  
  5. //  Created by luoge on 12-11-19.  
  6. //  Copyright (c) 2012年 watsy. All rights reserved.  
  7. //  
  8.   
  9. #import "CWLWConnectManager.h"  
  10.   
  11. static CWLWConnectManager *_pConnectionMgr_ = nil;  
  12.   
  13. @interface CWConnItem : NSObject <ASIHTTPRequestDelegate>  
  14. @property (nonatomic, assign) connectType cType;  
  15. @property (nonatomic, assign) NSInteger nTag;  
  16. @property (nonatomic, strong) NSString  *sKey;  
  17. @property (nonatomic, assign) id delegate;  
  18. @property (nonatomic, strong) NSString *sURL;  
  19. @property (nonatomic, strong) NSDictionary *params;  
  20. @property (nonatomic, strong) ASIHTTPRequest *httpReq;  
  21.   
  22. + (CWConnItem *) connWithtype:(connectType) ct  
  23.                           url:(NSString *) sl  
  24.                        params:(NSDictionary *) pd  
  25.                           Tag:(NSInteger) nt  
  26.                           key:(NSString *) sk  
  27.                      delegate:(id) del;  
  28.   
  29. - (CWConnItem *) initWithtype:(connectType) ct  
  30.                           url:(NSString *) sl  
  31.                        params:(NSDictionary *) pd  
  32.                           Tag:(NSInteger) nt  
  33.                           key:(NSString *) sk  
  34.         blockWithOutTagAndKey:(void(^)(BOOL bSuccess,id returnData,NSError *error)) blockWithOutTagAndKey  
  35.               blockWithOutKey:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag)) blockWithOutKey  
  36.                         block:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag, NSString *skey)) block;  
  37. - (void) start;  
  38.   
  39. @end  
  40.   
  41. @implementation CWConnItem  
  42. @synthesize nTag,sKey,delegate,sURL,cType,params;  
  43. @synthesize httpReq;  
  44.   
  45. - (void) dealloc {  
  46.     [httpReq clearDelegatesAndCancel];  
  47.     [httpReq release];  
  48.       
  49.     [params release];  
  50.     [sURL release];  
  51.     [sKey release];  
  52.     [super dealloc];  
  53. }  
  54.   
  55. + (CWConnItem *) connWithtype:(connectType) ct  
  56.                           url:(NSString *) sl  
  57.                        params:(NSDictionary *) pd  
  58.                           Tag:(NSInteger) nt  
  59.                           key:(NSString *) sk  
  60.                      delegate:(id) del {  
  61.     CWConnItem *item = [[CWConnItem alloc] init];  
  62.     item.nTag = nt;  
  63.     item.sKey = sk;  
  64.     item.sURL = sl;  
  65.     item.delegate = del;  
  66.     item.cType = ct;  
  67.     item.params = pd;  
  68.     return [item autorelease];  
  69. }  
  70.   
  71. - (CWConnItem *) initWithtype:(connectType) ct  
  72.                           url:(NSString *) sl  
  73.                        params:(NSDictionary *) pd  
  74.                           Tag:(NSInteger) nt  
  75.                           key:(NSString *) sk  
  76.         blockWithOutTagAndKey:(void(^)(BOOL bSuccess,id returnData,NSError *error)) blockWithOutTagAndKey  
  77.               blockWithOutKey:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag)) blockWithOutKey  
  78.                         block:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag, NSString *skey)) block {  
  79.     if (self = [super init]) {  
  80.         self.nTag = nt;  
  81.         self.sKey = sk;  
  82.         self.sURL = sl;  
  83.         self.cType = ct;  
  84.         self.params = pd;  
  85.           
  86.         if (cType == connectType_GET) {  
  87.             self.httpReq = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:sURL]];  
  88.             [httpReq setRequestMethod:@"GET"];  
  89.         } else if (cType == connectType_POST) {  
  90.             self.httpReq = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:sURL]];  
  91.             [httpReq setRequestMethod:@"POST"];  
  92.             ASIFormDataRequest *form = (ASIFormDataRequest *) httpReq;  
  93.             for (NSString *paramKey in [params allKeys]) {  
  94.                 [form addPostValue:[params objectForKey:paramKey] forKey:paramKey];  
  95.             }  
  96.         }  
  97.   
  98.         [httpReq setCompletionBlock:^(void){  
  99.             //如果没有tag和key  
  100.             if (blockWithOutTagAndKey) {  
  101.                 blockWithOutTagAndKey(YES, httpReq.responseString, nil);  
  102.             }  
  103.             //如果没有key  
  104.             if (blockWithOutKey) {  
  105.                 blockWithOutKey(YES, httpReq.responseString, nil, nTag);  
  106.             }  
  107.               
  108.             if (block) {  
  109.                 block(YES, httpReq.responseString, nil, self.nTag, self.sKey);  
  110.             }  
  111.               
  112.             if([[CWLWConnectManager sharedInstance] respondsToSelector:@selector(didFinishedWithItems:error:)]) {  
  113.                 [[CWLWConnectManager sharedInstance] performSelector:@selector(didFinishedWithItems:error:)  
  114.                                                           withObject:self  
  115.                                                           withObject:nil];  
  116.             }  
  117.         }];  
  118.   
  119.         [httpReq setFailedBlock:^(void) {  
  120.             if (blockWithOutTagAndKey) {  
  121.                 blockWithOutTagAndKey(NO, httpReq.responseString, httpReq.error);  
  122.             }  
  123.               
  124.             if (blockWithOutKey) {  
  125.                 blockWithOutKey(NO, httpReq.responseString, httpReq.error, nTag);  
  126.             }  
  127.               
  128.             if (block) {  
  129.                 block(NO, httpReq.responseString, httpReq.error, self.nTag, self.sKey);  
  130.             }  
  131.   
  132.             if([[CWLWConnectManager sharedInstance] respondsToSelector:@selector(didFinishedWithItems:error:)]) {  
  133.                 [[CWLWConnectManager sharedInstance] performSelector:@selector(didFinishedWithItems:error:)  
  134.                                                           withObject:self  
  135.                                                           withObject:httpReq.error];  
  136.             }  
  137.         }];  
  138.           
  139.         [httpReq start];  
  140.     }  
  141.     return self;  
  142. }  
  143.   
  144. - (void) start {  
  145.     NSAssert((sURL != nil), @"url can't be nil");  
  146.       
  147.     sURL = [sURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];  
  148.     if (cType == connectType_GET) {  
  149.         httpReq = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:sURL]];  
  150.         [httpReq setRequestMethod:@"GET"];  
  151.     } else if (cType == connectType_POST) {  
  152.         httpReq = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:sURL]];  
  153.         [httpReq setRequestMethod:@"POST"];  
  154.         ASIFormDataRequest *form = (ASIFormDataRequest *) httpReq;  
  155.         for (NSString *paramKey in [params allKeys]) {  
  156.             [form addPostValue:[params objectForKey:paramKey] forKey:paramKey];  
  157.         }  
  158.     }  
  159.     httpReq.delegate = self;  
  160.     [httpReq startAsynchronous];  
  161. }  
  162.   
  163. - (BOOL) isEqual:(id)object {  
  164.     CWConnItem *item = (CWConnItem *)object;  
  165.     if (![self.sURL isEqualToString:item.sURL]) {  
  166.         return NO;  
  167.     }  
  168.     if (self.cType != item.cType) {  
  169.         return NO;  
  170.     }  
  171.     if (self.delegate != item.delegate) {  
  172.         return NO;  
  173.     }  
  174.     if (self.nTag != item.nTag) {  
  175.         return NO;  
  176.     }  
  177.     if (self.sKey != nil && item.sKey != nil &&  
  178.         ![self.sKey isEqualToString:item.sKey]) {  
  179.         return NO;  
  180.     }  
  181.     for (NSString *paramKey in self.params) {  
  182.         id sp1 = [self.params objectForKey:paramKey];  
  183.         id sp2 = [self.params objectForKey:paramKey];  
  184.         if (sp2 == nil) {  
  185.             return NO;  
  186.         }  
  187.         if (![sp1 isEqual:sp2]) {  
  188.             return NO;  
  189.         }  
  190.     }  
  191.     return YES;  
  192. }  
  193.   
  194. - (void)requestFinished:(ASIHTTPRequest *)request {  
  195.     if([[CWLWConnectManager sharedInstance] respondsToSelector:@selector(didFinishedWithItems:error:)]) {  
  196.         [[CWLWConnectManager sharedInstance] performSelector:@selector(didFinishedWithItems:error:)  
  197.                                                   withObject:self  
  198.                                                   withObject:nil];  
  199.     }  
  200. }  
  201. - (void)requestFailed:(ASIHTTPRequest *)request {  
  202.     if([[CWLWConnectManager sharedInstance] respondsToSelector:@selector(didFinishedWithItems:error:)]) {  
  203.         [[CWLWConnectManager sharedInstance] performSelector:@selector(didFinishedWithItems:error:)  
  204.                                                   withObject:self  
  205.                                                   withObject:request.error];  
  206.     }  
  207. }  
  208.   
  209. @end  
  210.   
  211.   
  212. #pragma mark - CWLWConnectManager  
  213. @interface CWLWConnectManager()  
  214. @property (nonatomic, strong) NSMutableDictionary *connItems;  
  215.   
  216. //移除元素  
  217. - (id) hasItem:(CWConnItem *) item;  
  218. - (void) removeItems:(CWConnItem *) conn;  
  219. - (void) didFinishedWithItems:(CWConnItem *) conn  
  220.                         error:(NSError *) error;  
  221.   
  222. @end  
  223.   
  224. @implementation CWLWConnectManager  
  225.   
  226. @synthesize connItems;  
  227.   
  228. - (id) init {  
  229.     if (_pConnectionMgr_) {  
  230.         return _pConnectionMgr_;  
  231.     }  
  232.     if (self = [super init]) {  
  233.         self.connItems = [NSMutableDictionary dictionary];  
  234.     }  
  235.     return self;  
  236. }  
  237. - (void) dealloc {  
  238.     [connItems release];  
  239.     [super dealloc];  
  240. }  
  241.   
  242. + (CWLWConnectManager *) sharedInstance {  
  243.     if (_pConnectionMgr_ == nil) {  
  244.         _pConnectionMgr_ = [[CWLWConnectManager alloc] init];  
  245.     }  
  246.     return _pConnectionMgr_;  
  247. }  
  248.   
  249. + (void) releaseInstance {  
  250.     if (_pConnectionMgr_ != nil) {  
  251.         [_pConnectionMgr_ release];  
  252.         _pConnectionMgr_ = nil;  
  253.     }  
  254. }  
  255.   
  256. - (NSUInteger) conURL:(NSString *) sURL  
  257.           connectType:(connectType) cType  
  258.                params:(NSDictionary *) params  
  259.              delegate:(id) del {  
  260.     return [self conURL:sURL  
  261.             connectType:cType  
  262.                  params:params  
  263.                delegate:del  
  264.                     tag:0];  
  265. }  
  266.   
  267. - (NSUInteger) conURL:(NSString *) sURL  
  268.           connectType:(connectType) cType  
  269.                params:(NSDictionary *) params  
  270.              delegate:(id) del  
  271.                   tag:(NSInteger) nTag {  
  272.     return [self conURL:sURL  
  273.             connectType:cType  
  274.                  params:params  
  275.                delegate:del  
  276.                     tag:nTag  
  277.                     key:nil];  
  278. }  
  279.   
  280. - (NSUInteger) conURL:(NSString *) sURL  
  281.           connectType:(connectType) cType  
  282.                params:(NSDictionary *) params  
  283.              delegate:(id) del  
  284.                   tag:(NSInteger) nTag  
  285.                   key:(NSString *) sKey {  
  286.       
  287.     CWConnItem *item = [CWConnItem connWithtype:cType  
  288.                                             url:sURL  
  289.                                          params:params  
  290.                                             Tag:nTag  
  291.                                             key:sKey  
  292.                                        delegate:del];  
  293.     if ([self hasItem:item]) {  
  294.         //重复调用方法  
  295.     }  
  296.     NSUInteger hashValue = [item hash];  
  297.     [self.connItems setObject:item forKey:[NSNumber numberWithUnsignedInteger:hashValue]];  
  298.     [item start];  
  299.   
  300.     return hashValue;  
  301. }  
  302.   
  303. //取消网络连接  
  304. - (BOOL) cancelWithHashValue:(NSUInteger) nItemHashValue {  
  305.     CWConnItem *conn = [self.connItems objectForKey:[NSNumber numberWithUnsignedInteger:nItemHashValue]];  
  306.     if (conn) {  
  307.         [conn.httpReq clearDelegatesAndCancel];  
  308.         [self.connItems removeObjectForKey:[NSNumber numberWithUnsignedInteger:nItemHashValue]];  
  309.     }  
  310.       
  311.     return YES;  
  312. }  
  313.   
  314. - (BOOL) cancelURL:(NSString *) sURL  
  315.        connectType:(connectType) cType  
  316.             params:(NSDictionary *) params  
  317.           delegate:(id) del  
  318.                tag:(NSInteger) nTag  
  319.                key:(NSString *) sKey {  
  320.     CWConnItem *item = [CWConnItem connWithtype:cType  
  321.                                             url:sURL  
  322.                                          params:params  
  323.                                             Tag:nTag  
  324.                                             key:sKey  
  325.                                        delegate:del];  
  326.     CWConnItem *existItem = [self hasItem:item];  
  327.     if (existItem != nil) {  
  328.         if (existItem.httpReq != nil) {  
  329.             [existItem.httpReq clearDelegatesAndCancel];  
  330.         }  
  331.         [self.connItems removeObjectForKey:[NSNumber numberWithUnsignedInteger:[existItem hash]]];  
  332.   
  333.         return NO;  
  334.     }  
  335.     return YES;  
  336. }  
  337.   
  338. - (NSUInteger) conURL:(NSString *) sURL  
  339.           connectType:(connectType) cType  
  340.                params:(NSDictionary *) params  
  341.                result:(void(^)(BOOL bSuccess,id returnData,NSError *error)) block {  
  342.     CWConnItem *item = [[CWConnItem alloc] initWithtype:cType  
  343.                                                     url:sURL  
  344.                                                  params:params  
  345.                                                     Tag:0  
  346.                                                     key:nil  
  347.                                   blockWithOutTagAndKey:block  
  348.                                         blockWithOutKey:nil  
  349.                                                   block:nil];  
  350.     NSUInteger hashValue = [item hash];  
  351.     [self.connItems setObject:item forKey:[NSNumber numberWithUnsignedInteger:hashValue]];  
  352.     [item release];  
  353.       
  354.     return hashValue;  
  355. }  
  356.   
  357. - (NSUInteger) conURL:(NSString *) sURL  
  358.           connectType:(connectType) cType  
  359.                params:(NSDictionary *) params  
  360.                   tag:(NSInteger) nTag  
  361.                result:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag)) block {  
  362.     CWConnItem *item = [[CWConnItem alloc] initWithtype:cType  
  363.                                                     url:sURL  
  364.                                                  params:params  
  365.                                                     Tag:nTag  
  366.                                                     key:nil  
  367.                                   blockWithOutTagAndKey:nil  
  368.                                         blockWithOutKey:block  
  369.                                                   block:nil];  
  370.     NSUInteger hashValue = [item hash];  
  371.     [self.connItems setObject:item forKey:[NSNumber numberWithUnsignedInteger:hashValue]];  
  372.     [item release];  
  373.       
  374.     return hashValue;  
  375. }  
  376. - (NSUInteger) conURL:(NSString *) sURL  
  377.           connectType:(connectType) cType  
  378.                params:(NSDictionary *) params  
  379.                   tag:(NSInteger) nTag  
  380.                   key:(NSString *) sKey  
  381.                result:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag, NSString *skey)) block {  
  382.       
  383.     CWConnItem *item = [[CWConnItem alloc] initWithtype:cType  
  384.                                                     url:sURL  
  385.                                                  params:params  
  386.                                                     Tag:nTag  
  387.                                                     key:sKey  
  388.                                   blockWithOutTagAndKey:nil  
  389.                                         blockWithOutKey:nil  
  390.                                                   block:block];  
  391.     NSUInteger hashValue = [item hash];  
  392.     [self.connItems setObject:item forKey:[NSNumber numberWithUnsignedInteger:hashValue]];  
  393.     [item release];  
  394.       
  395.     return hashValue;  
  396. }  
  397.   
  398. #pragma mark - private action  
  399. - (id) hasItem:(CWConnItem *) conn {  
  400.     NSUInteger hashValue = [conn hash];  
  401.     id object = [self.connItems objectForKey:[NSNumber numberWithUnsignedInteger:hashValue]];  
  402.     if (object == nil) {  
  403.         for(id item in [self.connItems allValues]) {  
  404.             if ([conn isEqual:item]) {  
  405.                 return item;  
  406.             }  
  407.         }  
  408.     } else {  
  409.         return conn;  
  410.     }  
  411.     return nil;  
  412. }  
  413.   
  414. - (void) removeItems:(CWConnItem *) conn {  
  415.     NSUInteger hashValue = [conn hash];  
  416.     id object = [self.connItems objectForKey:[NSNumber numberWithUnsignedInteger:hashValue]];  
  417.     if (object != nil) {  
  418.         [[(CWConnItem *)object httpReq] clearDelegatesAndCancel];  
  419.         [self.connItems removeObjectForKey:[NSNumber numberWithUnsignedInteger:hashValue]];  
  420.     }  
  421. }  
  422.   
  423. - (void) didFinishedWithItems:(CWConnItem *) conn  
  424.                         error:(NSError *) error {  
  425.     if (error == nil) {  
  426.         if (conn.delegate && [conn.delegate respondsToSelector:@selector(didCWFinishSuccessedWithData:tag:key:)]) {  
  427.             [conn.delegate didCWFinishSuccessedWithData:conn.httpReq.responseString  
  428.                                                     tag:conn.nTag  
  429.                                                     key:conn.sKey];  
  430.         }  
  431.     } else {  
  432.         if (conn.delegate && [conn.delegate respondsToSelector:@selector(didCWFinishFailedWithError:tag:key:)]) {  
  433.             [conn.delegate didCWFinishFailedWithError:error  
  434.                                                   tag:conn.nTag  
  435.                                                   key:conn.sKey];  
  436.         }  
  437.     }  
  438.       
  439.     [self removeItems:conn];  
  440. }  
  441.   
  442. @end  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值