iphone平台http get请求

直接贴源码:

HttpGetRequestAppDelegate.h:


[plain]  view plain copy
  1. //  
  2. //  HttpGetRequestAppDelegate.h  
  3. //  HttpGetRequest  
  4. //  
  5. //  Created by apple on 12-5-25.  
  6. //  Copyright 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @class HttpGetRequestViewController;  
  12.   
  13. @interface HttpGetRequestAppDelegate : NSObject <UIApplicationDelegate>  
  14.   
  15. @property (nonatomic, retain) IBOutlet UIWindow *window;  
  16.   
  17. @property (nonatomic, retain) IBOutlet HttpGetRequestViewController *viewController;  
  18.   
  19. @end  


HttpGetRequestAppDelegate.m:


[plain]  view plain copy
  1. <span style="font-family:Arial,Helvetica,sans-serif;">  
  2. </span><pre name="code" class="plain">//  
  3. //  HttpGetRequestAppDelegate.m  
  4. //  HttpGetRequest  
  5. //  
  6. //  Created by apple on 12-5-25.  
  7. //  Copyright 2012年 __MyCompanyName__. All rights reserved.  
  8. //  
  9.   
  10. #import "HttpGetRequestAppDelegate.h"  
  11.   
  12. #import "HttpGetRequestViewController.h"  
  13.   
  14. @implementation HttpGetRequestAppDelegate  
  15.   
  16. @synthesize window = _window;  
  17. @synthesize viewController = _viewController;  
  18.   
  19. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  20. {  
  21.     // Override point for customization after application launch.  
  22.        
  23.     self.window.rootViewController = self.viewController;  
  24.     [self.window makeKeyAndVisible];  
  25.     return YES;  
  26. }  
  27.   
  28. - (void)applicationWillResignActive:(UIApplication *)application  
  29. {  
  30.     /*  
  31.      Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.  
  32.      Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.  
  33.      */  
  34. }  
  35.   
  36. - (void)applicationDidEnterBackground:(UIApplication *)application  
  37. {  
  38.     /*  
  39.      Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.   
  40.      If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.  
  41.      */  
  42. }  
  43.   
  44. - (void)applicationWillEnterForeground:(UIApplication *)application  
  45. {  
  46.     /*  
  47.      Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.  
  48.      */  
  49. }  
  50.   
  51. - (void)applicationDidBecomeActive:(UIApplication *)application  
  52. {  
  53.     /*  
  54.      Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.  
  55.      */  
  56. }  
  57.   
  58. - (void)applicationWillTerminate:(UIApplication *)application  
  59. {  
  60.     /*  
  61.      Called when the application is about to terminate.  
  62.      Save data if appropriate.  
  63.      See also applicationDidEnterBackground:.  
  64.      */  
  65. }  
  66.   
  67. - (void)dealloc  
  68. {  
  69.     [_window release];  
  70.     [_viewController release];  
  71.     [super dealloc];  
  72. }  
  73.   
  74. @end  


 

 

HttpGetRequestViewController.h

[plain]  view plain copy
  1. //  
  2. //  HttpGetRequestViewController.h  
  3. //  HttpGetRequest  
  4. //  
  5. //  Created by apple on 12-5-25.  
  6. //  Copyright 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface HttpGetRequestViewController : UIViewController  
  12. {  
  13.     //UIButton *button;  
  14.     UITextField *textView;  
  15.     NSMutableData *receiveData;  
  16. }  
  17.   
  18. //@property (nonatomic,retain) IBOutlet UIButton *button;  
  19. @property (nonatomic,retain) IBOutlet UITextField *textView;  
  20. @property (nonatomic,retain) NSMutableData *receiveData;  
  21.   
  22. - (IBAction)buttonPressed:(id)sender;  
  23.   
  24. - (void) sendRequestByGet:(NSString*)urlString;  
  25.   
  26. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;  
  27.   
  28. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;  
  29.   
  30. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;  
  31.   
  32. - (void)connectionDidFinishLoading:(NSURLConnection *)connection;  
  33.   
  34. @end  

HttpGetRequestViewController.m

[plain]  view plain copy
  1. //  
  2. //  HttpGetRequestViewController.m  
  3. //  HttpGetRequest  
  4. //  
  5. //  Created by apple on 12-5-25.  
  6. //  Copyright 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.   
  9. #import "HttpGetRequestViewController.h"  
  10.   
  11. @implementation HttpGetRequestViewController  
  12.   
  13. //@synthesize button;  
  14. @synthesize textView;  
  15. @synthesize receiveData;  
  16.   
  17. - (void)didReceiveMemoryWarning  
  18. {  
  19.     // Releases the view if it doesn't have a superview.  
  20.     [super didReceiveMemoryWarning];  
  21.       
  22.     // Release any cached data, images, etc that aren't in use.  
  23. }  
  24.   
  25. #pragma mark - View lifecycle  
  26.   
  27. /*  
  28. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
  29. - (void)viewDidLoad  
  30. {  
  31.     [super viewDidLoad];  
  32. }  
  33. */  
  34.   
  35. - (void)viewDidUnload  
  36. {  
  37.     [super viewDidUnload];  
  38.     // Release any retained subviews of the main view.  
  39.     // e.g. self.myOutlet = nil;  
  40.     self.textView = nil;  
  41.     self.receiveData = nil;  
  42. }  
  43.   
  44. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  45. {  
  46.     // Return YES for supported orientations  
  47.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  48. }  
  49.   
  50. - (IBAction)buttonPressed:(id)sender  
  51. {  
  52.     NSString *value = [NSString stringWithFormat:@"%@",textView.text];  
  53.     NSLog(@"URL:%@",value);  
  54.       
  55.     [self sendRequestByGet:value];  
  56.     //[value release];  
  57. }  
  58.   
  59. //http get implement  
  60. - (void) sendRequestByGet:(NSString*)urlString  
  61. {  
  62.     NSURL *url = [NSURL URLWithString:urlString];  
  63.     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];  
  64.     [request setHTTPMethod:@"GET"];  
  65.       
  66.     NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];  
  67.       
  68.     [request release];  
  69.     [conn release];  
  70. }  
  71.   
  72. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response  
  73. {  
  74.     self.receiveData = nil  
  75.     self.receiveData = [NSMutableData data]; //auto release?  
  76. }  
  77.   
  78. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data  
  79. {  
  80.     [self.receiveData appendData:data];  
  81. }  
  82.   
  83. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error  
  84. {  
  85.     NSLog(@"error=%@",[error localizedDescription]);  
  86. }  
  87.   
  88. - (void)connectionDidFinishLoading:(NSURLConnection *)connection  
  89. {  
  90.     NSString *result = [[NSString alloc] initWithBytes:[receiveData bytes] length:[receiveData length] encoding:NSUTF8StringEncoding];  
  91.       
  92.     NSLog(@"result=%@",result);  
  93.     [result release];  
  94. }  
  95.   
  96.   
  97. @end  

需要注意的是HttpGetRequestViewController 拥有NSMutableData *receiveData;这个成员。receiveDate是在:

(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

方法中通过self.receiveData = [NSMutableData data];获得的。因为不是通过alloc、new或copy创建的。所以receiveData是一个自动释放的对象。所以如果我想在当前事件循环结束后仍能用receiveData需要设置@property (nonatomic,retain) NSMutableData *receiveData;这样就可以在didReceiveResponse方法中通过self.receiveData = [NSMutableData data];使receiveData引用计数为2.

  但是存在的问题是: cocoa在程序开始处理事件之前创建了一个自动释放池。并在事件结束后销毁该自动释放池。当前事件循环结束或自动释放池被销毁是,receiveData会收到一条release消息。使receiveData引用计算为1.不执行销毁。下一次触发buttonPressed时,又会调用didReceiveResponse方法,receiveData又将获取到空对象。但是前一次receiveData里的内容并没有随着上一个自动释放池的回收而消失,这或许就是一个内存溢出吧。所以我在didReceiveResponse方法中增加:self.receiveData = nil;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值