Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

responseObject is not JSON but NSInLineData for AFHTTPRequestOperationManager

Following code is to submit images through an operationQueue. The requests are all fired one by one correctly, the server response contains the image file name which client needs to get hold of. The problem is that the reponseObject for the success/failure block is not expected parsed JSON but type of NSInLineData shown in debugger. Now I suspect the code to construct the operation from the NSMutableURLRequest caused the issue. Please help.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST"      
            URLString:podURLString parameters:nil
            constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

            NSError *error;
            BOOL success =[formData appendPartWithFileURL:imgURL name:@"images" fileName:img.path 
               mimeType:@"image/jpg" error:nil];
            if (!success)
                    NSLog(@"appendPartWithFileURL error: %@", error);} error:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
   NSLog(@"Image Success: %@", [responseObject description]);

   NSString *imagePath = [response objectForKey:@"imageFileName"];

   [self.delegate networkManager:self didSubmitDeliveryImageForImageID:imagePath];


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  NSLog(@"Image Error: %@", error);
  NSLog(@"image error: %@", [operation.responseObject description]);

 NSString *imageFilePath = [operation.responseObject objectForKey:@"imageFileName"];
 [self.delegate networkManager:self didFailSubmitDeliveryImageForImageID:imageFilePath];

}];
[manager.operationQueue addOperation:operation];
like image 391
xueru Avatar asked Nov 30 '14 05:11

xueru


2 Answers

When you get the response as NSInLineData. It's good to go now. You can write below single line of code to get NSDictionary if it supports json format.

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObjec options:0 error:nil];
like image 111
Amit Saxena Avatar answered Oct 14 '22 07:10

Amit Saxena


Just add this line of code before your AFHTTPRequestOperation block

**operation.responseSerializer = [AFJSONResponseSerializer serializer];**
like image 31
Muhammad_Awaab Avatar answered Oct 14 '22 07:10

Muhammad_Awaab