我是iphone開發新手。我已經發布了帶有用戶名和密碼的URL。我能夠在「連接didReceiveData」方法中打印數據。但是我看到「連接didReceiveData」方法被調用兩次。我不知道,我哪裏出錯了。這裏是我的代碼連接didReceiveData在iphone中發佈Url時調用兩次?
- (void)viewDidLoad {
[super viewDidLoad];
NSString *post = [NSString stringWithFormat:@"&domain=school.edu&userType=2&referrer=http://apps.school.edu/navigator/index.jsp&username=%@&password=%@",@"xxxxxxx",@"xxxxxx"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://secure.school.edu/login/process.do"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
NSLog(@"Connection Successful");
}
else
{
NSLog(@"Connection could not be made");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data{
NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"the data %@",string);
}
在控制檯中打印整個HTML頁面兩次,所以請幫助我。謝謝。
如果您收到大量數據,您可能需要查看NSFileHandler,以便在磁盤到達時將塊寫入磁盤。否則NSMutableData應該沒問題,特別是如果你不想存儲數據。 – FelixLam 2010-03-15 10:37:27
好點,菲利克斯 - 尤其是關於iPhone的有限內存。 – 2010-03-15 14:02:21