1

我在我的应用程序中遇到了一个奇怪的问题。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 秒?)。我试图改变它,但它没有改变任何东西。

知道为什么吗?

提前致谢。

4

1 回答 1

4

试试这个代码:

NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1200.0]; 

代替

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

您可以更改超时间隔。

于 2013-02-25T11:30:14.340 回答