我的应用中遇到了一个奇怪的问题。 NSURLConnection经常进入以下委托方法(超时发生前请求后几秒):
- (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秒?)。我试图改变它,但它没有改变任何东西。
知道为什么吗?
提前致谢。
答案 0 :(得分:3)
试试这段代码:
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1200.0];
而不是
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
您可以更改timeoutinterval。