我有一个iPad应用程序可以从服务器下载音乐文件m4a。我正在使用AFHTTPRequestOperation直接下载到我的doc dir uisng outputStreamToFileAtPath。我不担心需要多长时间。我只需要下载文件,因为下载很可能会在下班后发生。当我尝试在我的ipad上运行它时,我收到以下错误消息。我确实得到了前5和后5,但剩下的时间超时了。我的代码有问题吗?有没有办法增加超时值?或者除了AFNetworking之外还有什么我可以使用的吗?非常感谢任何帮助/想法。
ERROR ERROR ERROR:错误域= NSURLErrorDomain代码= -1001“请求超时。” UserInfo = 0xc6e01c0 {NSErrorFailingURLStringKey = http://xxxx/Music/ece0b7c5ab71a24c6f6694986fc7a4a7.m4a,NSErrorFailingURLKey = http://xxx/Music/ece0b7c5ab71a24c6f6694986fc7a4a7.m4a,NSLocalizedDescription =请求超时。,NSUnderlyingError = 0xc69c950“请求超时。” } - 无法保存到路径:/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];
}
答案 0 :(得分:0)
[request setTimeoutInterval:1000];
为interval设置一个合适的值(可以使用它),在创建请求后添加它。