2012-08-08 42 views
0

下面的函数不从服务器下载内容,它给出ASIHTTPRequestErrorDomain代码= 4”请求被取消。ASIHTTPRequestErrorDomain代码= 4“请求被取消

这个函数是从viewdidload调用的,但是相同的url正常工作,并且它正在下载内容 - (void)downLoad:(id)发件人事件:(id)事件函数,但不在 - (void)startDownload函数。

请让我知道是什么问题/错误的事情在我的功能( - (无效)startDownload)

以下功能工作正常

-(void)downLoad:(id)sender event:(id)event{ 

self.tblViewDownload.userInteractionEnabled = NO; 

IsRequestCompleted = NO; 

CGPoint touchPosition = [[[event allTouches] anyObject] locationInView:self.tblViewDownload]; 
NSIndexPath *indexPath = [self.tblViewDownload indexPathForRowAtPoint:touchPosition]; 
UITableViewCell *Cell = [self.tblViewDownload cellForRowAtIndexPath:indexPath]; 

progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar]; 
progress.frame = CGRectMake(65, 55, 210, 25); 
progress.progress = 0.0; 
[Cell.contentView addSubview:progress]; 

UIButton* button = (UIButton*)sender; 
button.hidden=YES; 

if(!self.networkQueue) 
    self.networkQueue = [[[ASINetworkQueue alloc] init] autorelease]; 

[self.networkQueue cancelAllOperations]; 
[self.networkQueue setQueueDidFinishSelector:@selector(queueCompleted:)]; 
[self.networkQueue setShowAccurateProgress:YES]; 
[self.networkQueue setDownloadProgressDelegate:progress]; 

[self.networkQueue setDelegate:self]; 

NSDictionary *aDict =[self.myUrlArray objectAtIndex:[indexPath row]]; 
NSString *aImgUrl = [aDict objectForKey:@"IMG_URL"]; 
NSString *aVideoUrl = [aDict objectForKey:@"VIDEO_URL"]; 
NSString *aAudioUrl = [aDict objectForKey:@"AUDIO_URL"]; 

NSArray *aPoemArrayUrls = [NSArray arrayWithObjects:aImgUrl,aVideoUrl,aAudioUrl,nil]; 

for(NSString* urlString in aPoemArrayUrls) 
{ 
    NSURL *url = [NSURL URLWithString:urlString]; 
    ASIHTTPRequest *downloadAFileRequest = [[ASIHTTPRequest requestWithURL:url]retain]; 
    NSString *Filename = [urlString lastPathComponent]; 
    NSLog(@"%@ filename",Filename); 
    [downloadAFileRequest setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]]; 
    [downloadAFileRequest setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:Filename]]; 
    [downloadAFileRequest setShouldContinueWhenAppEntersBackground:YES]; 
    [downloadAFileRequest setDelegate:self]; 
    [downloadAFileRequest setDidFinishSelector:@selector(requestDone:)]; 
    [downloadAFileRequest setDidFailSelector:@selector(requestWentWrong:)]; 
    [downloadAFileRequest setShowAccurateProgress:YES]; 

    [self.networkQueue addOperation:downloadAFileRequest]; 
    //---- 
} 

[self.networkQueue go]; 

} 

以下功能不工作并给出ASIHTTPRequestErrorDomain代码= 4“请求被取消

-(void)startDownload{ 

if([self.myUrlArray count] > 0){ 

int count = [self.myUrlArray count]; 

int i = count - count; 

NSLog(@"%d iiiiii",i); 

NSIndexPath *myIP = [NSIndexPath indexPathForRow:i inSection:0]; 

UITableViewCell *Cell = [self.tblViewDownload cellForRowAtIndexPath:myIP]; 

progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar]; 
progress.frame = CGRectMake(65, 55, 210, 25); 
progress.progress = 0.0; 
progress.backgroundColor = [UIColor redColor]; 
[Cell.contentView addSubview:progress]; 

    if(!self.networkQueue) 
     self.networkQueue = [[[ASINetworkQueue alloc] init] autorelease]; 

    [self.networkQueue cancelAllOperations]; 
    [self.networkQueue setQueueDidFinishSelector:@selector(queueCompleted:)]; 
    [self.networkQueue setShowAccurateProgress:YES]; 
    [self.networkQueue setDownloadProgressDelegate:progress]; 

    [self.networkQueue setDelegate:self]; 

    NSDictionary *aDict =[self.myUrlArray objectAtIndex:[myIP row]]; 
    NSString *aImgUrl = [aDict objectForKey:@"IMG_URL"]; 
    NSString *aVideoUrl = [aDict objectForKey:@"VIDEO_URL"]; 
    NSString *aAudioUrl = [aDict objectForKey:@"AUDIO_URL"]; 

    NSArray *aPoemArrayUrls = [NSArray arrayWithObjects:aImgUrl,aVideoUrl,aAudioUrl,nil]; 

    for(NSString* urlString in aPoemArrayUrls) 
    { 
     NSURL *url = [NSURL URLWithString:urlString]; 
     ASIHTTPRequest *downloadAFileRequest = [[ASIHTTPRequest requestWithURL:url]retain]; 
     NSString *Filename = [urlString lastPathComponent]; 
     NSLog(@"%@ filename",Filename); 
     [downloadAFileRequest setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]]; 
     [downloadAFileRequest setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:Filename]]; 
     [downloadAFileRequest setShouldContinueWhenAppEntersBackground:YES]; 
     [downloadAFileRequest setDelegate:self]; 
     [downloadAFileRequest setDidFinishSelector:@selector(requestDone:)]; 
     [downloadAFileRequest setDidFailSelector:@selector(requestWentWrong:)]; 
     [downloadAFileRequest setShowAccurateProgress:YES]; 

     [self.networkQueue addOperation:downloadAFileRequest]; 
    } 

    [self.networkQueue go]; 


} 
} 

回答

2

我想你忘了在第二个函数中设置IsRequestCompleted = NO;。另外,你为什么要发送cancelAllOperations?这是一个很大的机会成为问题。

此外,没有必要自动发布self.networkQueue并每次重新初始化它。只需为该课程设置一次,然后在-dealloc中发布。

+1

也不好主意直接添加到单元格[Cell.contentView addSubview:progress];重复使用会破坏你的逻辑 – NeverBe 2012-08-08 14:01:32

+0

同意NeverBe,没有抓住那个。 – 2012-08-08 14:10:04