iphone - iPhone SDK中的ASIHTTP请求已取消错误

标签 iphone ios asihttprequest image-uploading

我正在使用ASIHTTP request方法上传多个图像数据。所有图像成功上传,但仅最后一个图像ASIHttp请求失败。我尝试了很多,但是我再也受不了了。

有人能帮我吗?

我的代码如下:

for(int i=0;i<[arySteps count];i++)
{
     NSMutableArray *StepDetail=[[NSMutableArray alloc] initWithArray:[DatabaseAccess getAddSteps:str]];
     if([[[StepDetail objectAtIndex:0] valueForKey:@"s_image"] length]!=0)
     {
          NSMutableArray *imgary=[[[[StepDetail objectAtIndex:0] valueForKey:@"s_image"] componentsSeparatedByString:@","] mutableCopy];

                    imagedata1=[[NSData alloc] init];
                    imagedata2=[[NSData alloc] init];
                    imagedata3=[[NSData alloc] init];

                    for (int i=0; i<[imgary count]; i++)
                    {
                        if(i==0)
                        {
                            NSString *filename=[NSString stringWithFormat:@"%@.jpeg",[imgary objectAtIndex:0]];
                            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                            NSString *documentsDirectory = [paths objectAtIndex:0];
                            NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filename];
                            NSURL *movieURL = [NSURL fileURLWithPath:filePath];
                            imagedata1=[NSData dataWithContentsOfURL:movieURL];
                            NSLog(@"%@",imagedata1);
                        }
                        else if(i==1)
                        {
                            NSString *filename=[NSString stringWithFormat:@"%@.jpeg",[imgary objectAtIndex:1]];
                            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                            NSString *documentsDirectory = [paths objectAtIndex:0];
                            NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filename];
                            NSURL *movieURL = [NSURL fileURLWithPath:filePath];
                            imagedata2=[NSData dataWithContentsOfURL:movieURL];
                            NSLog(@"%@",imagedata2);
                        }
                        else if(i==2)
                        {
                            NSString *filename=[NSString stringWithFormat:@"%@.jpeg",[imgary objectAtIndex:2]];
                            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                            NSString *documentsDirectory = [paths objectAtIndex:0];
                            NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filename];
                            NSURL *movieURL = [NSURL fileURLWithPath:filePath];
                            imagedata3=[NSData dataWithContentsOfURL:movieURL];
                            NSLog(@"%@",imagedata3);
                        }
                    }

                    NSString *strurl=[NSString stringWithFormat:@"http://inst.niftysol.com/app/webroot/webservices/test.php?said=%d&stepid=%@", appdel.idPARENTID,s_id_live];

                    [self setHttprequest:[ASIFormDataRequest requestWithURL:[NSURL URLWithString:strurl]]];
                    //NSString *userid=[NSString stringWithFormat:@"%d",appdel.idUID];
                    // [httprequest setPostValue:userid forKey:@"s_user_id"];
                    [httprequest setShouldContinueWhenAppEntersBackground:YES];
                    [httprequest setDelegate:self];
                    [httprequest setDidFinishSelector:@selector(uploadFinished:)];
                    [httprequest setDidFailSelector:@selector(uploadFailed:)];
                    [httprequest setData:imagedata1 withFileName:@"1.jpeg" andContentType:@"image/jpeg" forKey:@"userfile1"];
                    [httprequest setData:imagedata2 withFileName:@"2.jpeg" andContentType:@"image/jpeg" forKey:@"userfile2"];
                    [httprequest setData:imagedata3 withFileName:@"3.jpeg" andContentType:@"image/jpeg" forKey:@"userfile3"];
                    countupload=countupload+1;
                    [httprequest startAsynchronous];

                }
}

在上面的代码中,我已经正确获取了所有图像数据,但是对于最后一个图像请求失败了。我得到错误:

错误域= ASIHTTPRequestErrorDomain代码= 4“请求已取消” UserInfo = 0x96fbfe0 {NSLocalizedDescription =请求已取消}

最佳答案

如果可能,您应该切换到 AFNetworking ,非常容易集成。尽管回答了您的问题,但是您应该切换到ASIHTTPRequest的网络队列。

在头文件中声明ASINetworkQueue *networkQueue;,声明属性@property (retain) ASINetworkQueue *networkQueue;并在实现文件中对其进行合成。

-(void)doUploadOperation //You can call this method for your upload operation.
{
    [[self networkQueue] cancelAllOperations];
    // Creating a new queue each time we use it means we don't have to worry about clearing delegates or resetting progress tracking
    [self setNetworkQueue:[ASINetworkQueue queue]];
    [[self networkQueue] setDelegate:self];
    [[self networkQueue] setRequestDidFinishSelector:@selector(requestFinished:)];
    [[self networkQueue] setRequestDidFailSelector:@selector(requestFailed:)];
    [[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];

    int i;
    for (i=0; i<[arySteps count]; i++)
    {
        //First create image data
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filename=[NSString stringWithFormat:@"%@.jpeg",[imgary objectAtIndex:i]];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filename];
        NSURL *movieURL = [NSURL fileURLWithPath:filePath];
        NSData *imagedata=[NSData dataWithContentsOfURL:movieURL];

        //Create request and add to network queue
        NSString *strurl=[NSString stringWithFormat:@"http://inst.niftysol.com/app/webroot/webservices/test.php?said=%d&stepid=%@", appdel.idPARENTID,s_id_live];
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strurl]];
        [request setShouldContinueWhenAppEntersBackground:YES];
        [request setData:imagedata withFileName:[NSString stringWithFormat:@"%d.jpeg",i+1] andContentType:@"image/jpeg" forKey:[NSString stringWithFormat:@"userfile%d",i+1]];
        request.tag = i;
        [[self networkQueue] addOperation:request];
    }
    [[self networkQueue] go];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    if ([[self networkQueue] requestsCount] == 0) {
        [self setNetworkQueue:nil];
    }
    //... Handle success
    NSLog(@"Individual Request finished");
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    if ([[self networkQueue] requestsCount] == 0) {
        [self setNetworkQueue:nil];
    }
    NSLog(@"Individual Request failed");
}

- (void)queueFinished:(ASINetworkQueue *)queue
{
    if ([[self networkQueue] requestsCount] == 0) {
        [self setNetworkQueue:nil];
    }
    NSLog(@"Whole Queue finished");
}

希望这可以帮助。如果您需要更多帮助,请随时发送消息。

关于iphone - iPhone SDK中的ASIHTTP请求已取消错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16206723/

相关文章:

iphone - - (void)applicationWillResignActive :(UIApplication *)application never gets called

javascript - HTML5 音频 : Music does not stop even after navigating away from the website

iphone - 普遍设置没有标题的自定义后退栏按钮

iphone - ASIHTTPRequest https 请求 & SSL

iphone - 取消操作时 ASIHTTPRequest 和 NSOperationQueue 崩溃

ios - ASIHTTP 运行 javascript 代码吗?

ios - NaN 会导致这个核心音频 iOS 应用程序偶尔崩溃吗?

iPhone:更改 UITextView 中的行距?

ios - 在 UserInfo 中存储多个 UILocalNotification

ios - 使用segue传递图像时指针类型不兼容? objective-c Xcode