草庐IT

ios - AFNetworking AFHTTPRequestOperation block 从未被调用

coder 2024-01-13 原文

我正在使用 AFNetworking 将多部分表单发送到 Web 服务器,但我的 AFHTTPRequestOperation 遇到了一些问题。在我启动它之后,它的成功和失败 block 永远不会被调用。

这是我的代码(它的简历)

    NSMutableURLRequest *request = [[ServerAPI sharedClient] multipartFormRequestWithMethod:@"POST" path:postUrl parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
        [formData appendPartWithFileData:picture.picture_data name:@"InputFile" fileName:picture.name mimeType:@"image/jpg"];
    }];

    AFHTTPRequestOperation *operation = [[ServerAPI sharedClient] HTTPRequestOperationWithRequest: request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success");
    } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure");
    }];

    [operation setUploadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        NSLog(@"%f", (totalBytesRead / (float) totalBytesExpectedToRead));
    }];         
    [[ServerAPI sharedClient] enqueueHTTPRequestOperation:operation];

我可以看到进度日志,但从未调用成功和失败 block 。

picture.picture_data 是用 UIImageJPEGRepresentation(image, 0.7) 初始化的 NSData ServerAPI是AFHTTPClient的子类,sharedClient是单例方法。

AFNetworking 不调用我的 block 的原因是什么,甚至没有正确的错误消息?

谢谢大家!

编辑

我在这个请求之前使用相同的 URL 发出了一个获取请求,它像往常一样工作。我使用的 URL 是:part/_layouts/UploadEx.aspx?List=%7BD432BF97-7175-40C1-8E0D-27D8661CBC90%7D&RootFolder=%2Fpwa%2Fpart%2FLibrary&Source=http%3A%2F%2Fwww%2Emysite %2Ecom%2Fpwa%2Fpart%2FLibrary%2FForms%2FAllItems%2Easpx&IsDlg=1

最佳答案

在您的代码中,检查您的 postUrlBaseURL+postURL 必须有效。尝试使用普通网络浏览器使用 URL BaseURL+postURL 上传图片。

编辑

方法 HTTPRequestOperationWithRequest:success:failure: 不适用于文件上传,但适用于 json/html 获取。 尝试使用

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

[operation setUploadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        NSLog(@"%f", (totalBytesRead / (float) totalBytesExpectedToRead));
    }];

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

[[ServerAPI sharedClient] enqueueHTTPRequestOperation:operation];

关于ios - AFNetworking AFHTTPRequestOperation block 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14929115/

有关ios - AFNetworking AFHTTPRequestOperation block 从未被调用的更多相关文章

随机推荐