objc afnetworking 2.0在出错时显示responseObject

时间:2013-11-21 15:23:01

标签: ios objective-c post upload afnetworking-2

我通过POST request将图片上传到server。成功后,我得到JSON回复。不幸的是发生了错误。我想将其删除,但我不知道如何显示出现错误时从服务器获得的响应。我想看JSON text

did not start with array or object and option to allow fragments not set.

如何使用AFNetworking 2.0显示失败时POST请求的响应体(而不是错误正文)?

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

2 个答案:

答案 0 :(得分:0)

你的意思是this吗?我不确定它是否真的得到了解决。

答案 1 :(得分:0)

NSString *errorBodyMessage = [[error userInfo] objectForKey:@"NSLocalizedRecoverySuggestion"];