【问题标题】:ASIHTTPRequest set Download Destination Path failingASIHTTPRequest 设置下载目标路径失败
【发布时间】:2012-08-16 11:17:59
【问题描述】:

我有一个查找某个 pdf 的方法,如果它在本地找不到它,它会使用 ASIHTTPRequest 异步下载它。但是,当以下行时,请求总是失败: [request setDownloadDestinationPath:currentDetailItem]; 未注释,请求开始并且进度增加,直到 100% 然后执行请求失败的块。

这些是请求失败时的相关 NSLog:

2012-08-16 12:08:34.398 XXXX[1675:707] Request started with url :http://XXXX.com/gwas/sites/default/files/Responding%20to%20Severe%20Weather%20Events.pdf
filePath :/var/mobile/Applications/322C24CF-9664-403D-9CC5-13C396F39F84/Documents/Responding%20to%20Severe%20Weather%20Events.pdf
2012-08-16 12:08:39.018 XXXX[1675:707] Request failed:HTTP/1.1 200 OK

这里是方法的代码:

- (void)setDetailItem:(NSString *)newDetailItem {
    NSArray *downloadPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSArray *components = [newDetailItem componentsSeparatedByString:@"/"];
    NSString *filePath = [[downloadPaths objectAtIndex:0] stringByAppendingFormat:@"/%@", [components lastObject]];
    currentDetailItem = filePath;

    if (![self fileExistsLocally:[components lastObject]]) {
        //Download the file
        [self displayProgressView];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:newDetailItem]];
        [request setDownloadDestinationPath:currentDetailItem];
        [request setDownloadProgressDelegate:progressBar];
        [request setCompletionBlock:^
         {
             [self showPdf:currentDetailItem];
             [self hideProgressView];
             NSLog(@"%f, Request finished :%@", progressBar.progress, request.responseStatusMessage);
         }];
        [request setFailedBlock:^
         {
             NSLog(@"Request failed:%@", request.responseStatusMessage);
             [self hideProgressView];
             [SVProgressHUD showErrorWithStatus:@"Request failed"];
         }];
        [request startAsynchronous];

        NSLog(@"Request started with url :%@\nfilePath :%@", newDetailItem, currentDetailItem);
    }
    else {
        [self showPdf:currentDetailItem]; 
    }
}

如果我将[request setDownloadDestinationPath:currentDetailItem];这一行注释掉,则请求成功。有任何想法吗?谢谢!

【问题讨论】:

  • 大概您可以单步执行并查看 /why/ asi 正在调用失败方法。

标签: ios ipad asihttprequest


【解决方案1】:

对于任何感兴趣的人,我通过切换到 NSURLConnection 解决了这个问题,代码如下:

- (void)setDetailItem:(NSString *)newDetailItem {
    NSArray *downloadPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSArray *components = [newDetailItem componentsSeparatedByString:@"/"];
    NSString *filePath = [[downloadPaths objectAtIndex:0] stringByAppendingFormat:@"/%@", [components lastObject]];
    currentDetailItem = filePath;

    if (![self fileExistsLocally:[components lastObject]]) {
        [self displayProgressView];
        data_ = [[NSMutableData alloc] init];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:newDetailItem] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
        if (!connection) {
            [SVProgressHUD showErrorWithStatus:@"Request failed"];
            return;
        }
        else {
            data_ = [[NSMutableData alloc] init];
        }
    }
    else {
        [self showPdf:currentDetailItem]; 
    }
}

#pragma mark NSURLConnectionDelegate methods

- (void)connection: (NSURLConnection*) connection didReceiveResponse: (NSHTTPURLResponse*) response
{
    if ([response statusCode] == 200) {
        currentDownloadSize = [response expectedContentLength];
    }
    [data_ setLength:0];
}

- (void) connection: (NSURLConnection*) connection didReceiveData: (NSData*) data
{
    [data_ appendData:data];
    progressBar.progress = ((float) [data_ length] / (float) currentDownloadSize);

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //Save file locally
    NSURL *url = [NSURL fileURLWithPath:currentDetailItem];
    NSError *error = nil;
    [data_ writeToURL:url options:0 error:&error];
    if (error) {
        NSLog(@"Write failed with error:%@", error);
    }
    else {
        NSLog(@"Request successful");
        [self showPdf:currentDetailItem];
    }

    [self hideProgressView];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    [self hideProgressView];
    [SVProgressHUD showErrorWithStatus:@"Request failed"];
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

【讨论】:

    猜你喜欢
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 2021-02-22
    相关资源
    最近更新 更多