直接贴源码:
httpGetRequestAppDelegate.h:
[plain] view plain copy // // 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; @end
httpGetRequestAppDelegate.m:
httpGetRequestVIEwController.h
copy // httpGetRequestVIEwController.h @interface httpGetRequestVIEwController : UIVIEwController //UIbutton *button; UITextFIEld *textVIEw; NSMutableData *receiveData; //@property (nonatomic,retain) IBOutlet UIbutton *button; @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
copy // httpGetRequestVIEwController.m #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 @H_502_593@
需要注意的是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;
总结以上是内存溢出为你收集整理的iphone平台http get请求全部内容,希望文章能够帮你解决iphone平台http get请求所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)