gpt4 book ai didi

iphone - AFJSONRequestOperation 和文件数据的 AFNetworking 永远不会完成

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:05:37 26 4
gpt4 key购买 nike

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

有人有使用 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];

最佳答案

我在许多应用程序上使用此代码并且没有遇到任何问题(注意我使用的是 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]];

}];

成功 block (我的 API 返回 stat -ok 或失败,然后是数据(与 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];

关于iphone - AFJSONRequestOperation 和文件数据的 AFNetworking 永远不会完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14415093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com