0

我意识到这是一个模糊的问题,但我想知道是否有其他人观察到这一点。这是我调用 NSURLConnection 的代码

   // Get data from server
    NSString *host = @"www.hostname.com";            
    NSString *urlString = [NSString stringWithFormat:@"/theRestOfTheURL"];
    NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:host path:urlString];

    DLog(@"URL is %@", url);

    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    receivedData_ = [[NSMutableData data] retain];

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:15.0];

    // create the connection with the request
    // and start loading the data
    self.powerPlantDataConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease];

    [url release]; 

当我第一次加载应用程序时,它运行良好,我可以重复调用它而没有任何问题。但如果我关闭应用程序并重新打开它,

(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

每次都会调用委托方法,并显示请求超时错误消息。我有两个不同的视图控制器,我在其中调用两个不同的 URL,并且每次关闭并重新打开应用程序后它们都会失败。

谁能想到为什么会发生这种情况?我不确定从哪里开始寻找。请求超时错误的原因可能是什么?该请求应该没有任何问题,因为它在我第一次运行应用程序时有效。


编辑补充说,我似乎只在我的设备上遇到这个问题,而不是在模拟器上。

4

2 回答 2

3

希望您共享了一些 chrash 日志(尤其是具有明确定义的 [error localizedDescription]类方法..)

正如您所说,它将超时(您的请求)。而且由于您创建对象的方式太杂乱,因此您的系统工作量更大。我建议在下载数据时使用GCD,尤其是在像您这样的情况下,具有不同的接口和 url..

一个建议 您可以像这样创建您的 url 对象:

   NSURL *url = [NSURL urlWithString:[NSString stringWithFormat:@"http://%@/%@?key1=%@&key2=%@", yourDomain, targetFile, value1, value2]];
于 2012-07-11T15:07:41.257 回答
0

I switched over to using ASIHTTPRequest, which improved things but I would still get stuck in situations where I was getting timed out errors every time I tried to refresh. I looked and asked around, and eventually found that disabling calls to TestFlight solved my issue. More information here:

ASIHTTPRequest request times out

and here:

github.com/pokeb/asi-http-request/issues/320

于 2012-07-24T18:19:44.557 回答