我的应用程序出现了一个奇怪的问题。NSURL连接经常转到以下委托方法(请求后几秒钟,超时发生(:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if (DEBUG_ON)
NSLog(@"No connection");
UIAlertView *popup = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:NSLocalizedString(@"TimeoutError", nil) delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
popup.alertViewStyle = UIAlertViewStyleDefault;
[popup show];
}
我的所有连接都是这样的:
NSString *dataStr = [NSString stringWithFormat:LOGIN_JSON_TEMPLATE, email, passwd];
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:LOGIN_WS];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
if (self.loginConnection == nil)
self.loginConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
else
self.loginConnection = [self.loginConnection initWithRequest:request delegate:self];
如果我是对的,那么请求会有一个默认的超时(60秒?(。我试着改变它,但它没有改变任何东西。
知道为什么吗?
提前谢谢。
试试这个代码:
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1200.0];
代替
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
并且您可以更改timeoutinterval。