2012-08-08 74 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