当我连接到端口号我正在连接以测试时间时,我的应用程序冻结了。
我打电话给[request setTimeoutInterval:10];
,我认为应该是10秒。但是,应用程序挂起。它可能与本地服务器有关吗?
代码:
// call this when program first starts
-(void) nSendMessage : (NSString *) name Password: (NSString *) password page: (NSString *) page
{
// set the url
NSString *address = @"http://localhost:1075/update";
address=[ address stringByAppendingString: page];
NSMutableURLRequest *request =
[[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:address]];
// post or get
[request setHTTPMethod:@"POST"];
// data to send
NSString *postString = @"username=iusername&password=ipassword";
NSString *sendString=[postString stringByReplacingOccurrencesOfString:@"iusername" withString: name];
sendString=[sendString stringByReplacingOccurrencesOfString:@"ipassword" withString: password];
[request setValue:[NSString
stringWithFormat:@"%d", [sendString length]]
forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[sendString
dataUsingEncoding:NSUTF8StringEncoding]];
[request setTimeoutInterval:10];
[[NSURLConnection alloc]
initWithRequest:request delegate:self];
//
//THE PROGRAM FREEZES HERE
//
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *response = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
// Phrase repl
[self nUpdateDisplay:response];
}
答案 0 :(得分:0)
你的问题看起来像是这个问题:
iPhone SDK: URL request not timing out
总结一下:即使传入较小的值,超时下限至少为4分钟。
如上面的链接所详述,使用异步方法通常是最佳选择。如果您使用同步,就像上面所做的那样,并且您在主runloop中,您将完全阻止您的UI,这将导致“挂起”效果。