我正在使用 NSURLConnection sendAsynchronousRequest 从 Web 服务器获取一些数据。问题是有时我能够获取数据,有时会从 Web 服务器返回 NULL。当我查看服务器日志时,它已经发送了数据。我怀疑这是一个时间问题。下面是我的代码。
if(netStatus==ReachableViaWiFi || netStatus==ReachableViaWWAN)
{
NSLog(@"YES WIFI");
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[NSURL URLWithString:formattedURL];
[request setURL:[NSURL URLWithString:formattedURL]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *jsonData, NSError *error)
{
progressbar.progress = 0.0;
NSLog(@"Next line of code");
NSLog(@"dataAsString %@", [NSString stringWithUTF8String:[jsonData bytes]]);
if ([NSString stringWithUTF8String:[jsonData bytes]]==NULL)
{
NSLog(@"R U HERE...THE RESULT IS NULL");
UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *viewController = [storybord instantiateViewControllerWithIdentifier:@"noserver"];
viewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:viewController animated:YES];
}
else
{
searchTable.hidden=FALSE;
progressbar.hidden=TRUE;
NSLog(@"R U HERE...THE RESULT IS NOT NULL");
NSError *error1;
dictresult = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error1];
self.searchTable.dataSource=self;
self.searchTable.delegate=self;
[searchTable reloadData];
}
NSLog(@"Asynch request in search results finished!");
if (error) {
} else {
}
}];
}
是否有在期望来自 Web 服务器的数据时设置的默认超时。我可以增加默认超时时间还是可以指示等待更多时间,直到我从服务器接收到数据。请让我知道如何完成这项任务。谢谢你的时间