0

I upload an image via POST request to an server. On success I get a JSON answer back. Unfortunatly an error occurs. I would like to takle this down, but I do not know how to show the respsonse I get from the server when an error occurs. I would like to see the JSON text which did not start with array or object and option to allow fragments not set.

How do I show the responsebody (not error-body) of an POST request on failure, by using AFNetworking 2.0?

Error Message:
> 2013-11-21 16:05:16.479 Foobar[1772:907] -[CameraViewController upload]: uploader  
> 2013-11-21 16:05:27.275 Foobar[1772:907] Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x2085aee0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

CameraViewController.h

#import <UIKit/UIKit.h>

@interface CameraViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

CameraViewControllerView.m

#import "CameraViewController.h"
#import "AFHTTPRequestOperationManager.h"

@interface CameraViewController ()

@property (nonatomic) int photoIsTaken;

@end

@implementation CameraViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.photoIsTaken = 0;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

}

-(void)viewDidAppear:(BOOL)animated {
    if (self.photoIsTaken == 0) {
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            // select Photo
        } else {
            [self takePhoto];
        }
    } else {
        [self upload];
    }

}

- (void)takePhoto {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:NO completion:NULL];
}

// [ removed unecessary code for this question ]

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;
    self.photoIsTaken = 1;

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)upload {
    NSLog(@"%s: uploader ", __FUNCTION__);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"latitude": @"8.444444",
                                 @"longitude": @"50.44444",
                                 @"location": @"New York",
                                 @"type": @"2",
                                 @"claim": @"NYC",
                                 @"flag": @"0",
                                 @"file": UIImageJPEGRepresentation(self.imageView.image,30)};

    [manager POST:@"http://192.168.1.157/tapp/laravel/public/foobar/upload" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    [self dismissViewControllerAnimated:NO completion:nil];
}

@end
Sport
  • 8,570
  • 6
  • 46
  • 65
jerik
  • 5,714
  • 8
  • 41
  • 80
  • 1
    What is operation.responseData? Is it nil/empty? – Erik Kerber Nov 21 '13 at 15:40
  • 1
    When an error occurs the failure block is called. The data returned by the server should be in operation.responseData – Joshua Lückers Nov 21 '13 at 15:43
  • @JoshuaLückers and @ErikKerber, operation.responseData is filled. Thanks. But I get a strange response: `<3c21444f 43545950 45206874 6d6c3e0a 3c68746 ... 3c2f6874 6d6c3e>`. No Glue what this is... – jerik Nov 22 '13 at 10:31
  • 1
    using `operation.responseString`, gives me the `human readable` output. operation.responseData is from type NSDATA, that perhaps why i had the - in my eyes - strange output... – jerik Nov 22 '13 at 13:49

2 Answers2

0

Do you mean this? I'm not sure it was actually resolved.

Ethan Mick
  • 9,517
  • 14
  • 58
  • 74
0
NSString *errorBodyMessage = [[error userInfo] objectForKey:@"NSLocalizedRecoverySuggestion"];
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Irfan Khatik
  • 146
  • 7