AFNetworking与AFJSONRequestOperation和filedata永远不会完成

时间:2013-01-19 13:59:55

标签: iphone ios afnetworking

我正在使用AFNetworking将json数据发布到我的webservice并获得json响应。但是现在我还要为这些POST添加multipart formdata。如果我这样做(即使没有我先添加的参数),完成块也不会触发。进度块会触发,我可以看到文件正确上传。

有没有人有经验使用AFNetworking发布这样的图像?我使用的是最新的AFNetworking源/版本。

这是我用json发布字典的初始代码。这很好。

NSMutableDictionary *postdata = [[NSMutableDictionary alloc] init];
[postdata setObject:[postdictionary JSONString] forKey:@"request"];

NSMutableURLRequest *jsonRequest = [httpClient requestWithMethod:@"POST"
                                           path:path
                                     parameters:postdata];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:jsonRequest success:^(NSURLRequest *request,     NSHTTPURLResponse *response, id JSON) {// Success} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {// Failure }];

[operation start];

这是提交图像的代码版本(不起作用)

NSMutableURLRequest *jsonRequest jsonRequest = [httpClient   multipartFormRequestWithMethod:@"POST" path:path parameters:postdata constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData name:@"photo" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
    [formData throttleBandwidthWithPacketSize:5000 delay:0.1];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:jsonRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long  totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Upload succes");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Upload failed");
}];

[operation start];

2 个答案:

答案 0 :(得分:3)

我在许多应用程序上使用此代码并且没有遇到任何问题(请注意我使用的是AFJSONRequestOperation和AFRestClient)

设置请求

// I have a shared singleton in a separate file APIClient.h 
AFRESTClient *httpClient = [APIClient sharedClient];

NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:method parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) 
    {[formData appendPartWithFileData:imageData
               name:@"image"
               fileName:@"image.png"
               mimeType:@"image/png"];
    }];

然后是操作,

AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];

我的进度块

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
    CGFloat progress = ((CGFloat)totalBytesWritten) / totalBytesExpectedToWrite * 100;
    [SVProgressHUD showProgress:50 status:[NSString stringWithFormat:@"%0.1f %%", progress]];

}];    

成功阻止(我的API返回stat -ok或fail,然后是数据(与flickr API相同)

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id response) {
    NSLog(@"response string: %@", operation.responseString);
    NSString *resultStat = [response objectForKey:@"stat"];

   if ([resultStat isEqualToString:@"ok"]) {
        //success
        // do something with your data that is returned from the API 
   } else {
        //error 
        NSLog(@"Data Error: %@", response);
   }   

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", [error localizedDescription]); 
}];

[httpClient enqueueHTTPRequestOperation:operation];

答案 1 :(得分:0)

看不到代码的任何问题,几乎没有可能:

尝试设置短暂超时间隔并检查请求是否完成         [request setTimeoutInterval:5];

还尝试评论限制线,看看它是否会改变任何内容