当前位置:编程学习 > wap >>

ASIHTTPRequest足够了吗?

 大家都知道ASIHTTPRequest系列的类很好用,功能很强大。但也像NSURLConnection一样,一个网络请求就需要实例化一个对象,而且无法避免重复请求的问题。
        我个人不习惯用ASIHTTPRequest,因为它功能太强大了,函数太多了,而我们平时的项目仅仅是处理基于HTTP协议的网络交互,用ASIHTTPRequest未免太“奢侈”了。更重要的是,它并无法避免网络的重复请求的问题,而且无法控制网络请求数量,所以仅仅使用ASIHTTPRequest还不够。
        事实上,用NSURLConnection足够了。下面是我写的封装
HTTPConnection.h
 1. <span style="font-size:16px;">// 
2. //  HTTPConnection.h 
3. //   
4. // 
5. //  Created by Jianhong Yang on 12-1-3. 
6. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 
7. // 
8.  
9. #import <Foundation/Foundation.h> 
10.  
11.  
12. #define  MAXNUMBER_HTTPCONNECTION           30 
13.  
14. @protocol HTTPConnectionDelegate; 
15.  
16. @inte易做图ce HTTPConnection : NSObject { 
17. @private 
18.     // 
19.     int _numberOfHTTPConnection; 
20.     NSMutableArray *_marrayTaskDic; 
21.      
22.     id <HTTPConnectionDelegate> _delegate; 
23. } 
24.  
25. @property (nonatomic, assign) id <HTTPConnectionDelegate> delegate; 
26.  
27. // 根据URL获取Web数据 
28. // dicParam 
29. //    type:int,请求类型 
30. - (BOOL)requestWebDataWithURL:(NSString *)strURL andParam:(NSDictionary *)dicParam; 
31.  
32. // 根据URLRequest获取Web数据 
33. // dicParam 
34. //    type:int,请求类型 
35. - (BOOL)requestWebDataWithRequest:(NSURLRequest *)request andParam:(NSDictionary *)dicParam; 
36.  
37. //取消网络请求 
38. - (BOOL)cancelRequest:(NSDictionary *)dicParam; 
39.  
40. //清空网络请求 
41. - (void)clearRequest; 
42.  
43. @end 
44.  
45.  
46. @protocol HTTPConnectionDelegate <NSObject> 
47.  
48. @optional 
49.  
50. // 网络数据下载失败 
51. - (void)httpConnect:(HTTPConnection *)httpConnect error:(NSError *)error with:(NSDictionary *)dicParam; 
52.  
53. // 服务器返回的HTTP信息头 
54. - (void)httpConnect:(HTTPConnection *)httpConnect receiveResponseWithStatusCode:(NSInteger)statusCode  
55.  andAllHeaderFields:(NSDictionary *)dicAllHeaderFields with:(NSDictionary *)dicParam; 
56.  
57. // 服务器返回的部分数据 
58. - (void)httpConnect:(HTTPConnection *)httpConnect receiveData:(NSData *)data with:(NSDictionary *)dicParam; 
59.  
60. // 网络数据下载完成 
61. - (void)httpConnect:(HTTPConnection *)httpConnect finish:(NSData *)data with:(NSDictionary *)dicParam; 
62.  
63. @end 
64.  
65.  
66. #ifdef DEBUG 
67.  
68. #define HTTPLOG(fmt,...)     NSLog((@"HTTP->%s(%d):"fmt),__PRETTY_FUNCTION__,__LINE__,##__VA_ARGS__) 
69.  
70. #else 
71.  
72. #define HTTPLOG(fmt,...)     NSLog(fmt,##__VA_ARGS__) 
73.  
74. #endif</span> 

HTTPConnection.m
[plain] view plaincopy
1. // 
2. //  HTTPConnection.m 
3. //   
4. // 
5. //  Created by Jianhong Yang on 12-1-3. 
6. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 
7. // 
8.  
9. #import "HTTPConnection.h" 
10.  
11.  
12. @inte易做图ce HTTPConnection (Private) 
13. - (void)startHTTPConnection; 
14. @end 
15.  
16.  
17. @implementation HTTPConnection 
18.  
19. @synthesize delegate = _delegate; 
20.  
21. - (id)init 
22. { 
23.     self = [super init]; 
24.     if (self) { 
25.         // Custom initialization. 
26.         _numberOfHTTPConnection = 0; 
27.         _marrayTaskDic = [[NSMutableArray alloc] initWithCapacity:5]; 
28.     } 
29.     return self; 
30. } 
31.  
32. - (void)dealloc 
33. { 
34.     //清空任务 
35.     [self clearRequest]; 
36.     // 
37.     [_marrayTaskDic release]; 
38.      
39.     [super dealloc]; 
40. } 
41.  
42.  
43. #pragma mark - 
44. #pragma mark Public 
45.  
46. // 根据URL获取Web数据 
47. // dicParam 
48. //    type:int,请求类型 
49. - (BOOL)requestWebDataWithURL:(NSString *)strURL andParam:(NSDictionary *)dicParam 
50. { 
51.     if (nil == dicParam) { 
52.         return NO; 
53.     } 
54.      
55.     NSURL *url = [NSURL URLWithString:strURL]; 
56.     NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; 
57.     BOOL success = [self requestWebDataWithRequest:request andParam:dicParam]; 
58.     [request release]; 
59.     return success; 
60. } 
61.  
62. // 根据URLRequest获取Web数据 
63. // dicParam 
64. //    type:int,请求类型 
65. - (BOOL)requestWebDataWithRequest:(NSURLRequest *)request andParam:(NSDictionary *)dicParam 
66. { 
67.     if (nil == dicParam) { 
68.         return NO; 
69.     } 
70.     //请求类型必须存在 
71.     if (nil == [dicParam objectForKey:@"type"]) { 
72.         HTTPLOG(@"任务参数不足"); 
73.         return NO; 
74.     } 
75.&nb

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,