Objective-c代码 复制代码 收藏代码
  1. //调用方式 
  2.  
  3. Ajax *ajax = [[Ajax alloc]Ajax:urlStr  
  4.         target:self  
  5. didFinish:@selector(showData:)
  6.         isAllValues:NO  
  7.         valueForKey:@"list" 
  8.         showProgressBar:YES]; 
  9. [ajax release]; 
  10.  
  11. //异步回调方法 
  12. -(void) showData:(NSArray*)data{ 
  13.    NSLog(@"data:%@",data); 
  14. } 

Objective-c代码 复制代码 收藏代码
  1. // 
  2. //  Ajax.h 
  3. //  live 
  4. // 
  5. //  Created by xjj xjj on 11-7-28. 
  6. //  Copyright 2011 新境界. All rights reserved. 
  7. // 
  8.  
  9. #import <Foundation/Foundation.h> 
  10. #import "UIProgressBar.h" 
  11.  
  12. @interface Ajax : NSObject { 
  13.  
  14. } 
  15. @property(nonatomic,assign) id target; 
  16. @property(nonatomic)SEL didFinish; 
  17. @property(nonatomic,retain) NSMutableData *buf; 
  18. @property(nonatomic,retain) NSURLConnection *connection; 
  19. @property(nonatomic,assign) BOOL isAllValues; 
  20. @property(nonatomic,assign) NSString* valueForKey; 
  21. @property(nonatomic,assign) BOOL isText; 
  22. @property(nonatomic,assign) NSString *urlStr; 
  23. @property(nonatomic,retain) UIProgressBar *progressBar; 
  24. @property(nonatomic,assign) long contentLength; 
  25. @property(nonatomic,assign) BOOL showProgressBar; 
  26.  
  27. -(void)start; 
  28. -(void)hiddenProgreesBar; 
  29.  
  30. /*! 
  31. @method Ajax:target:didFinish:isAllValues:valueForKey: 
  32. @discussion 异步加载某个节点数据(JSON格式) 
  33. @param _urlStr 网络数据URL 
  34. @param _target 调用者 
  35. @param _didFinish 数据加载完毕后通知动作 
  36. @param _isAllValues 是否返回所有数据 NO or YES 
  37. @param _valueForKey 加载某个节点数据 
  38. @result 初始化Ajax实例,并异步执行 
  39. */ 
  40. -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey; 
  41.  
  42. /*! 
  43. @method Ajax:target:didFinish: 
  44. @discussion 异步加数据(文件本格式)所有文本数据 
  45. @param _urlStr 网络数据URL 
  46. @param _target 调用者 
  47. @param _didFinish 数据加载完毕后通知动作 
  48. @result 初始化Ajax实例,并异步执行 
  49. */ 
  50. -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish; 
  51.  
  52. /*! 
  53. @method Ajax:target:didFinish:isAllValues:valueForKey:showProgressBar: 
  54. @discussion 异步加载某个节点数据(JSON格式) 
  55. @param _urlStr 网络数据URL 
  56. @param _target 调用者 
  57. @param _didFinish 数据加载完毕后通知动作 
  58. @param _isAllValues 是否返回所有数据 NO or YES 
  59. @param _valueForKey 加载某个节点数据 
  60. @param _showProgressBar 是否显示进度条 NO or YES 
  61. @result 初始化Ajax实例,并异步执行 
  62. */ 
  63. -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey showProgressBar:(BOOL)_showProgressBar; 
  64.  
  65. /*! 
  66. @method Ajax:target:didFinish:showProgressBar 
  67. @discussion 异步加数据(文件本格式)所有文本数据 
  68. @param _urlStr 网络数据URL 
  69. @param _target 调用者 
  70. @param _didFinish 数据加载完毕后通知动作 
  71. @param _showProgressBar 是否显示进度条 NO or YES 
  72. @result 初始化Ajax实例,并异步执行 
  73. */ 
  74. -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish showProgressBar:(BOOL)_showProgressBar; 
  75.  
  76. @end 

实现

Objective-c代码 复制代码 收藏代码
  1. // 
  2. //  Ajax.m 
  3. //  live 
  4. // 
  5. //  Created by xjj xjj on 11-7-28. 
  6. //  Copyright 2011 新境界. All rights reserved. 
  7. // 
  8.  
  9. #import "Ajax.h" 
  10. #import "JSONParser.h" 
  11. #import "UIProgressBar.h" 
  12.  
  13. @implementation Ajax 
  14. @synthesize target,didFinish; 
  15. @synthesize buf; 
  16. @synthesize connection; 
  17. @synthesize isAllValues,valueForKey; 
  18. @synthesize isText; 
  19. @synthesize urlStr; 
  20. @synthesize progressBar; 
  21. @synthesize contentLength; 
  22. @synthesize showProgressBar; 
  23.  
  24. -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish showProgressBar:(BOOL)_showProgressBar{ 
  25.     if(self){ 
  26.         self.showProgressBar = _showProgressBar; 
  27.         self.urlStr = _urlStr; 
  28.         self.isText = YES; 
  29.         self.target = _target; 
  30.         self.didFinish = _didFinish; 
  31.         [self start]; 
  32.     } 
  33.     return self; 
  34. } 
  35. -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish{ 
  36.     /*self = [super init]; 
  37.     if(self){ 
  38.         self.showProgressBar = YES; 
  39.         self.urlStr = _urlStr; 
  40.         self.isText = YES; 
  41.         self.target = _target; 
  42.         self.didFinish = _didFinish; 
  43.         [self start]; 
  44.     } 
  45.     return self;*/ 
  46.     return [self Ajax:_urlStr target:_target didFinish:_didFinish showProgressBar:YES]; 
  47. } 
  48.  
  49. -(id) Ajax:(NSString *)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey showProgressBar:(BOOL)_showProgressBar{ 
  50.     self = [super init]; 
  51.     if(self){ 
  52.         self.showProgressBar = _showProgressBar; 
  53.         self.urlStr = _urlStr; 
  54.         self.isAllValues = _isAllValues; 
  55.         self.valueForKey = _valueForKey; 
  56.         self.target = _target; 
  57.         self.didFinish = _didFinish; 
  58.         [self start]; 
  59.     } 
  60.     return self; 
  61. } 
  62.  
  63. -(id) Ajax:(NSString *)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey{ 
  64.     return [self Ajax:_urlStr target:_target didFinish:_didFinish isAllValues:_isAllValues valueForKey:_valueForKey showProgressBar:YES]; 
  65. } 
  66.  
  67. -(void)start{ 
  68.     if(connection==nil){ 
  69.         [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
  70.         buf = [[NSMutableData alloc] initWithLength:0]; 
  71.         NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; 
  72.         [request setURL:[NSURL URLWithString:urlStr]]; 
  73.         [request setHTTPMethod:@"GET"]; 
  74.         connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 
  75.          
  76.         if([target isKindOfClass:[UIViewController class]]==YES&&showProgressBar==YES){ 
  77.             UIViewController *viewController = (UIViewController*)target; 
  78.             progressBar = [[UIProgressBar alloc] initWithFrame:CGRectMake(0, viewController.view.frame.size.height - 10,viewController.view.frame.size.width, 10)]; 
  79.             progressBar.minValue = 0; 
  80.              
  81.             [progressBar setLineColor:[UIColor blackColor]]; 
  82.             [progressBar setProgressColor:[UIColor redColor]];   
  83.             //[progressBar setProgressColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"3.png"]]];  
  84.             [progressBar setProgressRemainingColor:[UIColor greenColor]]; 
  85.             [viewController.view addSubview:progressBar];    
  86.         } 
  87.         [request release]; 
  88.     } 
  89. } 
  90. //收到响应时 
  91. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
  92.     if(progressBar!=nil) 
  93.         progressBar.maxValue = response.expectedContentLength; 
  94. } 
  95. //接收数据 
  96. -(void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data{ 
  97.     [buf appendData:data]; 
  98.     if(progressBar!=nil) 
  99.         progressBar.currentValue += [data length]; 
  100. } 
  101. //加载失败 
  102. -(void)connection:(NSURLConnection*)aConn didFailWithError:(NSError*)error{ 
  103.     NSLog(@"didFailWithError:%@",error); 
  104.     [self hiddenProgreesBar]; 
  105. } 
  106. //接收完毕 
  107. -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
  108.     if(didFinish!=nil){ 
  109.         if(isText) 
  110.             [target performSelector:didFinish withObject:buf]; 
  111.         else{ 
  112.             NSArray *data = [JSONParser parseJSON:buf isAllValues:isAllValues valueForKey:valueForKey]; 
  113.             [target performSelector:didFinish withObject:data]; 
  114.         } 
  115.     } 
  116.     [self hiddenProgreesBar]; 
  117. } 
  118. -(void) hiddenProgreesBar{ 
  119.     if(progressBar!=nil){ 
  120.     //淡淡消失效果 
  121.         [UIView animateWithDuration:2 
  122.                      animations:^{ 
  123.                          progressBar.alpha = 0; 
  124.                      }]; 
  125.         [progressBar performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2]; 
  126.     } 
  127.     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
  128. } 
  129. -(void)dealloc{ 
  130.     [connection release]; 
  131.     [buf release]; 
  132.     [progressBar release]; 
  133.     [super dealloc]; 
  134. } 
  135. @end 
posted on 2012-08-14 16:40  jackljf  阅读(199)  评论(0)    收藏  举报