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