0

我有一个 iPad 应用程序,可以从服务器下载音乐文件 m4a。我正在使用 AFHTTPRequestOperation 直接下载到我的文档目录 uisng outputStreamToFileAtPath。我不关心需要多长时间。我只需要下载文件,因为下载很可能会在几个小时后进行。当我尝试在我的 ipad 上运行它时,我收到以下错误消息。我确实得到了前 5 个和最后 5 个,但其余的超时。我的代码有问题吗?有没有办法增加超时值?或者除了AFNetworking还有什么我可以使用的吗?非常感谢任何帮助/想法。

ERROR ERROR ERROR:Error Domain=NSURLErrorDomain Code=-1001 "请求超时。" UserInfo=0xc6e01c0 {NSErrorFailingURLStringKey=http://xxxx/Music/ece0b7c5ab71a24c6f6694986fc7a4a7.m4a, NSErrorFailingURLKey=http://xxx/Music/ece0b7c5ab71a24c6f6694986fc7a4a7.m4a, NSLocalizedDescription=The request timed out., NSUnderlyingError=0xc69c950 "The request timed out." } - 无法保存到路径:/var/mobile/Applications/207B2EFB-78E0-4BB2-8019-026B598ECE44/Documents/music/ece0b7c5ab71a24c6f6694986fc7a4a7.m4a

和代码:

- (void)saveFilesToDocDir
{
    NSString *fileLink = @"http://xxx/Music/";
    NSArray *dirPathSearch = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirPath = [dirPathSearch objectAtIndex:0];
    NSString *dirPath = [docDirPath stringByAppendingPathComponent:@"music/"];

    // if the sub directory does not exist, create it
    NSError *error = nil;
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath:dirPath])
    {
        NSLog(@"%@: does not exists...will attempt to create", dirPath);

        if (![fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error])
        NSLog(@"errormsg:%@", [error description]);
    }

    self.processCount = 0;
    for (int i = 0; i < [self.musicFiles count]; i++)
    {
        NSString *filename = [self.musicFiles objectAtIndex:i];
        NSString *urlPath = [NSString stringWithFormat:@"%@%@", fileLink, filename];
        NSString *filePath = [dirPath stringByAppendingPathComponent:filename];

        // download the song file and save them directly to docdir
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlPath]];

        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
        {
            self.processCount++;
            NSLog(@"Song:%d Success!", processCount);

            // all the files have been saved, now update the playlist
            if (self.processCount == [self.musicFiles count])
            {
                [self updatePlaylist];
            }

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) 
        {
            self.processCount++;
            NSLog(@"ERROR ERROR ERROR:%@ - could not save to path:%@", error, filePath);
        } ];

        [operation start];
    }
4

1 回答 1

0
[request setTimeoutInterval:1000];

为间隔设置一个适当的值(可以使用这个),在请求创建后添加它。

于 2014-01-27T07:05:54.697 回答