带有AFJSONRequestOperation和文件数据的AFNetworking从未完成 [英] AFNetworking with AFJSONRequestOperation and filedata never completes

查看:88
本文介绍了带有AFJSONRequestOperation和文件数据的AFNetworking从未完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AFNetworking将json数据发布到我的网络服务中并获得json响应。但是,现在我也想将多部分表单数据添加到这些POST中。如果我这样做(即使没有先添加的参数),也不会触发完成块。进度块不会触发,我可以看到文件正确上传。

I'm using AFNetworking to post json data to my webservice and get a json response back. However now I also want to add multipart formdata to these POST's. If I do this (even without the parameters I added first) the completion block never fires. The progress block DOES fire and I can see that the file uploads correctly.

有人在AFNetworking上发布类似图像的经验吗?我正在使用最新的AFNetworking源代码/版本。

Does anybody have any experience posting images like this with AFNetworking? I am using the latest AFNetworking source / version.

这是我发布带有json的字典的初始代码。

This is my initial code for posting a dictionary with json in it. This works fine.

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

这是用于提交图像的代码版本(无效)

This is the version of the code for submitting an image (doesn't work)

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


推荐答案

我在许多应用程序和Haven上使用此代码遇到任何问题(请注意,我正在使用AFJSONRequestOperation和AFRestClient)

I use this code on a number of apps and havent had any problems with it (note I am using the AFJSONRequestOperation and 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相同) )

success block (my API returns a stat -ok or fail, then the data (same as 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];

这篇关于带有AFJSONRequestOperation和文件数据的AFNetworking从未完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆