Ios 使用AFNetworking 2.0异常解析JSON

Ios 使用AFNetworking 2.0异常解析JSON,ios,objective-c,afnetworking-2,Ios,Objective C,Afnetworking 2,我在使用AFNetworking处理JSON解析数据时遇到了一个问题 这是我的密码: AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; NSDictionary *parameters = @{@"st

我在使用AFNetworking处理JSON解析数据时遇到了一个问题

这是我的密码:

        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSDictionary *parameters = @{@"strEmail": _logInEmail.text, @"strPassword":_logInPassword.text};
        [manager POST:@"http://carefid.com/api/user/login.php" parameters:parameters success:^

          (AFHTTPRequestOperation *operation, id responseObject) {


//NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
              NSString *str = responseObject[@"error"];
            NSLog(@"JSON: %@", str);
            UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"rootController"];
            [self presentViewController:myController animated:YES completion:nil];

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];
当我尝试记录该值时,出现以下错误:

2014-07-15 14:39:08.438 carefid[7858:60b] -[_NSInlineData objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1094c67a0
2014-07-15 14:39:08.441 carefid[7858:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSInlineData objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1094c67a0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101d48495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001019c399e objc_exception_throw + 43
    2   CoreFoundation                      0x0000000101dd965d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000101d39d8d ___forwarding___ + 973
    4   CoreFoundation                      0x0000000101d39938 _CF_forwarding_prep_0 + 120
    5   carefid                             0x000000010001f14f __34-[ViewController existingUserBtn:]_block_invoke + 95
    6   carefid                             0x0000000100015648 __64-[AFHTTPRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke46 + 40
    7   libdispatch.dylib                   0x0000000102669851 _dispatch_call_block_and_release + 12
    8   libdispatch.dylib                   0x000000010267c72d _dispatch_client_callout + 8
    9   libdispatch.dylib                   0x000000010266c3fc _dispatch_main_queue_callback_4CF + 354
    10  CoreFoundation                      0x0000000101da6289 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    11  CoreFoundation                      0x0000000101cf3854 __CFRunLoopRun + 1764
    12  CoreFoundation                      0x0000000101cf2d83 CFRunLoopRunSpecific + 467
    13  GraphicsServices                    0x00000001040bef04 GSEventRunModal + 161
    14  UIKit                               0x0000000100570e33 UIApplicationMain + 1010
    15  carefid                             0x0000000100049183 main + 115
    16  libdyld.dylib                       0x00000001028cd5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
什么会导致这个问题?responseObject是字典吗?
如果我的代码错误,我应该如何处理?

您正在将响应解析器设置为
AFHTTPResponseSerializer
,而不是
AFJSONResponseSerializer
。因此,响应永远不会被解析为JSON

所以改变

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

正如您在评论中提到的,服务器正在交付内容
text/html
,如果可能,服务器应该说内容是op-type
application/json
text/json
text/javascript

如果无法更改服务器内容类型,您可以告诉
AFJSONResponseSerializer
接受
text/html
如下:

AFJSONResponseSerializer *jsonReponseSerializer = [AFJSONResponseSerializer serializer]; 
// This will make the AFJSONResponseSerializer accept any content type
jsonReponseSerializer.acceptableContentTypes = nil;
manager.responseSerializer = jsonReponseSerializer;

2014-07-15 15:26:19.765 carefid[8036:60b]错误:错误域=com.alamofire.Error.serialization.response Code=-1016“请求失败:不可接受的内容类型:text/html”用户信息=0x10d924d10{com.alamofire.serialization.response.error.response=现在我收到了这个错误..您的服务器正在以
text/html
的形式向应用程序发送JSON内容,这是不正确的,如果可能的话,应该在服务器上更改为
application/JSON
text/JSON
text/javascript
有任何方法可以处理它站在我这边?如果服务器现在完成了,也就是发送大约500个服务器错误页面,JSON解析将尝试并解析它。这不是一个真正的问题,只是不需要,并且以后可能会带来一些困难。值得注意的是,
AFHTTPRequestOperationManager
上的默认
responseSerializer
AFJSONRes的一个实例ponseSerializer
AFJSONResponseSerializer *jsonReponseSerializer = [AFJSONResponseSerializer serializer]; 
// This will make the AFJSONResponseSerializer accept any content type
jsonReponseSerializer.acceptableContentTypes = nil;
manager.responseSerializer = jsonReponseSerializer;