永远不会调用AFNetworking AFHTTPRequestOperation块

时间:2013-02-18 03:40:35

标签: ios objective-c-blocks afnetworking nsmutableurlrequest

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

这是我的代码(简历)

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

我可以看到进度的日志,但永远不会调用成功和失败块。

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

AFNetworking不会拨打我的电话的原因是什么,即使没有正确的错误信息也是如此?

谢谢大家!

修改

我在此之前使用相同的URL执行get请求,并且照常工作。我使用的网址是: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

1 个答案:

答案 0 :(得分:0)

在您的代码中,检查您的postUrlBaseURL+postURL必须有效。尝试使用网址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];