直接贴源码:
HttpGetRequestAppDelegate.h:
- //
- // HttpGetRequestAppDelegate.h
- // HttpGetRequest
- //
- // Created by apple on 12-5-25.
- // Copyright 2012年 __MyCompanyName__. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @class HttpGetRequestViewController;
- @interface HttpGetRequestAppDelegate : NSObject <UIApplicationDelegate>
- @property (nonatomic, retain) IBOutlet UIWindow *window;
- @property (nonatomic, retain) IBOutlet HttpGetRequestViewController *viewController;
- @end
HttpGetRequestAppDelegate.m:
- <span style="font-family:Arial,Helvetica,sans-serif;">
- </span><pre name="code" class="plain">//
- // HttpGetRequestAppDelegate.m
- // HttpGetRequest
- //
- // Created by apple on 12-5-25.
- // Copyright 2012年 __MyCompanyName__. All rights reserved.
- //
- #import "HttpGetRequestAppDelegate.h"
- #import "HttpGetRequestViewController.h"
- @implementation HttpGetRequestAppDelegate
- @synthesize window = _window;
- @synthesize viewController = _viewController;
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- // Override point for customization after application launch.
- self.window.rootViewController = self.viewController;
- [self.window makeKeyAndVisible];
- return YES;
- }
- - (void)applicationWillResignActive:(UIApplication *)application
- {
- /*
- 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.
- 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.
- */
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
- /*
- 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.
- If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
- */
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- /*
- 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.
- */
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
- /*
- 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.
- */
- }
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- /*
- Called when the application is about to terminate.
- Save data if appropriate.
- See also applicationDidEnterBackground:.
- */
- }
- - (void)dealloc
- {
- [_window release];
- [_viewController release];
- [super dealloc];
- }
- @end
HttpGetRequestViewController.h
- //
- // HttpGetRequestViewController.h
- // HttpGetRequest
- //
- // Created by apple on 12-5-25.
- // Copyright 2012年 __MyCompanyName__. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface HttpGetRequestViewController : UIViewController
- {
- //UIButton *button;
- UITextField *textView;
- NSMutableData *receiveData;
- }
- //@property (nonatomic,retain) IBOutlet UIButton *button;
- @property (nonatomic,retain) IBOutlet UITextField *textView;
- @property (nonatomic,retain) NSMutableData *receiveData;
- - (IBAction)buttonPressed:(id)sender;
- - (void) sendRequestByGet:(NSString*)urlString;
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection;
- @end
HttpGetRequestViewController.m
- //
- // HttpGetRequestViewController.m
- // HttpGetRequest
- //
- // Created by apple on 12-5-25.
- // Copyright 2012年 __MyCompanyName__. All rights reserved.
- //
- #import "HttpGetRequestViewController.h"
- @implementation HttpGetRequestViewController
- //@synthesize button;
- @synthesize textView;
- @synthesize receiveData;
- - (void)didReceiveMemoryWarning
- {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- #pragma mark - View lifecycle
- /*
- // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- }
- */
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- self.textView = nil;
- self.receiveData = nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- - (IBAction)buttonPressed:(id)sender
- {
- NSString *value = [NSString stringWithFormat:@"%@",textView.text];
- NSLog(@"URL:%@",value);
- [self sendRequestByGet:value];
- //[value release];
- }
- //http get implement
- - (void) sendRequestByGet:(NSString*)urlString
- {
- NSURL *url = [NSURL URLWithString:urlString];
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
- [request setHTTPMethod:@"GET"];
- NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- [request release];
- [conn release];
- }
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- {
- self.receiveData = nil
- self.receiveData = [NSMutableData data]; //auto release?
- }
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- {
- [self.receiveData appendData:data];
- }
- - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- {
- NSLog(@"error=%@",[error localizedDescription]);
- }
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection
- {
- NSString *result = [[NSString alloc] initWithBytes:[receiveData bytes] length:[receiveData length] encoding:NSUTF8StringEncoding];
- NSLog(@"result=%@",result);
- [result release];
- }
- @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;