AFNetworking with AFJSONRequestOperation and filedata never completes

烂漫一生 提交于 2019-12-06 03:48:36

I use this code on a number of apps and havent had any problems with it (note I am using the AFJSONRequestOperation and AFRestClient)

set the request

// 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"];
    }];

then the operation,

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

my progress block

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

}];    

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

cannot see any issues with your code, few possibilities:

try setting short timeout interval and check if request completes [request setTimeoutInterval:5];

also try commenting out the throttling line and see if it changes anything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!