0

私は使用しており、サーバーにオブジェクトをAFNetworkingアップロードしたいと考えています。NSDataSOAP サーバーにアップロードしようとしているので、NSDataオブジェクトに変換した XML があります。何らかの理由で、以下は機能しません。

// Back to NSData
    NSData *convertedFile = [xml dataUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:siteConfiguration.soapURL];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
    NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
        [formData appendPartWithFormData:convertedFile name:assetCreation.name];
    }];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

        // Progress
        float totalProgress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
        progress(totalProgress, totalBytesWritten, totalBytesExpectedToWrite);
        if (totalBytesExpectedToWrite == totalBytesWritten) {

            completion();

        }
    }];
    [operation start];

ただし、multipartFormRequestWithMethodブロックを使用していない場合は機能し、ファイルは正しくアップロードされますが、コールバックが 1 回しか呼び出されないため、進行状況は表示されません。

// Back to NSData
NSData *convertedFile = [xml dataUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:siteConfiguration.soapURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod: @"POST"];
[request setHTTPBody:convertedFile];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

    // Progress
    float totalProgress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
    progress(totalProgress, totalBytesWritten, totalBytesExpectedToWrite);
    if (totalBytesExpectedToWrite == totalBytesWritten) {

        MatrixAsset *asset = [[MatrixAsset alloc] init];
        completion(asset);

    }
}];
[operation start];

私のサーバーは本当にHTTPBody私のNSDataオブジェクトと共に送信されることを望んでいるようです。AFNetworkingプログレスコールバックで同じことをすることは可能ですか?

4

2 に答える 2

0

わかりました、私はそれを働かせました。私はそれについて間違っていたようです:

// Setup the connection
    NSString *wsdlURL = [NSString stringWithFormat:@"%@?WSDL", siteConfiguration.soapURL];
    NSURL *serviceUrl = [NSURL URLWithString:wsdlURL];
    NSMutableURLRequest *serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
    [serviceRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
    [serviceRequest setHTTPMethod:@"POST"];
    [serviceRequest setHTTPBody:[xml dataUsingEncoding:NSUTF8StringEncoding]];

    // Operation
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:serviceRequest];
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

        // Progress
        float totalProgress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
        progress(totalProgress, totalBytesWritten, totalBytesExpectedToWrite);

    }];
    [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success");
        MatrixAsset *asset = [[MatrixAsset alloc] init];
        completion(asset);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@", error.localizedDescription);
        NSLog(@"error: %@", error.localizedFailureReason);
    }];
    [operation start];
于 2012-11-08T01:27:03.553 に答える