1

メソッドを使用して複数の画像データをアップロードしています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];

                }
}

上記のコードでは、すべての画像データを適切に取得していますが、最後の画像リクエストは失敗します。エラーが発生します:

エラー Domain=ASIHTTPRequestErrorDomain Code=4 「リクエストはキャンセルされました」 UserInfo=0x96fbfe0 {NSLocalizedDescription=リクエストはキャンセルされました}

4

1 に答える 1

3

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");
}

お役に立てれば。さらにサポートが必要な場合は、遠慮なくメッセージをお送りください。

于 2013-04-25T05:50:36.907 に答える