0

我有一个Nsurlsesiondownloadtask运行,用于下载本地托管的 XML 文件:url - http://10.232.254.59/catalogue.xml

问题是当我使用 url - http://10.232.254.59/catalogue.x或 url - http://10.232.254.59/catal 或任何其他组合而不更改 IP 时,didcompletewitherror委托给我 error= (null)。

//  MainViewController.m
@interface MainViewController ()
{   
    NSURLSessionDownloadTask *download;  
} 
@property(nonatomic,strong)NSURLSession *backgroundSession;

- (void)viewDidLoad {
  NSURLSessionConfiguration *ConfigurationObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    ConfigurationObject.timeoutIntervalForRequest = 10.0;
    ConfigurationObject.timeoutIntervalForResource = 10.0;
    self.backgroundSession = [NSURLSession sessionWithConfiguration:ConfigurationObject delegate:self delegateQueue:nil];

    [self LoadMediaUrl_StartTask_StartActivityIndicator];
}
-(void)LoadMediaUrl_StartTask_StartActivityIndicator{
    NSURL *url ;
    url = [self mediaUrlFromSettings];
    if(url.absoluteString.length != 0){
        download = [self.backgroundSession downloadTaskWithURL:url];
        [download resume];
        --do more stuff---
    }     
}
- (void)URLSession:(NSURLSession *)session downloadTask:(nonnull NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(nonnull NSURL *)location{

NSLog(@"didFinishDownloadingToURL called");

NSFileManager *fileManager = [NSFileManager defaultManager];
dispatch_async(dispatch_get_main_queue(), ^{
*** start the parser ***
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success" message:[NSString stringWithFormat:@" Catalogue Download Success! Press OK to Load New Media List."] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"cancel", nil];
    alert.tag = ALERT_VIEW_DOWNLOAD_SUCCESS;
    [alert show];
});


  -(void)URLSession:(NSURLSession *)session task:(nonnull NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error
{  

       NSLog(@"didCompleteWithError error :  %@",[error localizedDescription]);
    if(error != nil){

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.downloadActivityIndicator stopAnimating];
            [self.downloadProgressView setHidden:YES];
            self.DownloadImageView.alpha = 1.0;
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[NSString stringWithFormat:@" Catalogue Download Failed ! %s%@","\n",[error localizedDescription]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Retry", nil];
            alert.tag =ALERT_VIEW_DOWNLOAD_FAILED;
            [alert show];
        });
    }

    download = nil;
}

我在更改 IP 时得到正确的错误,didCompleteWithError但在更改 IP/* 时却没有,因为即使在错误 url 的情况下,也会显示下载成功警报。
即使在错误的 url 情况下,我也会收到didFinishDownloadingToURL委托呼叫。
LoadMediaUrl_StartTask_StartActivityIndicator在其他几个地方打电话并使用相同的 backgroundSession obj。

谁能告诉我可能是什么原因?

4

2 回答 2

0

我想我得到了原因 URLSession didCompleteWithError nil 错误

这是因为它只报告客户端错误,但不确定这种情况如何符合服务器问题。

于 2016-08-05T14:50:49.637 回答
0

要检测服务器错误(例如 404),请实现 didReceiveResponse 委托方法。从那里,检查 NSHTTPURLResponse 对象中的 httpStatusCode 属性。您必须首先将 NSURLResponse 转换为 NSHTTPURLResponse 指针。

于 2016-08-06T01:21:45.440 回答