简体   繁体   English

ASIHTTPRequestErrorDomain Code = 4“请求已被取消

[英]ASIHTTPRequestErrorDomain Code=4 "The request was cancelled

The below function is not downloading the content from server and it gives ASIHTTPRequestErrorDomain Code=4 "The request was cancelled. 以下功能不是从服务器下载内容,它提供ASIHTTPRequestErrorDomain Code = 4“请求被取消。

This function is called from viewdidload.but the same urls are working fine and it is downloading the content fine in the -(void)downLoad:(id)sender event:(id)event function but not in the -(void)startDownload function. 这个函数是从viewdidload调用的。但是相同的url工作正常, 它正在下载内容 - (void)downLoad:(id)发送者事件:(id)事件函数但不在 - (void)startDownload函数中。

Please let me know what is the problem / wrong thing in my function (-(void)startDownload) 请让我知道我的功能有什么问题/错误( - (void)startDownload)

below function is working fine 以下功能正常

-(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];

}

The below function is not working and gives ASIHTTPRequestErrorDomain Code=4 "The request was cancelled 以下功能无效,并提供ASIHTTPRequestErrorDomain Code = 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];


}
}

I think you forgot to set IsRequestCompleted = NO; 我想你忘了设置IsRequestCompleted = NO; in the second function. 在第二个功能。 Also, why are you sending cancelAllOperations? 另外,为什么要发送cancelAllOperations? That has a great chance of being the problem. 这很有可能成为问题所在。

Also, there is no need to autorelease self.networkQueue and reinitialize it every time. 此外,无需自动释放self.networkQueue并每次重新初始化它。 Just set it once for the class and release in -dealloc . 只需为类设置一次,然后在-dealloc释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM