2014-07-15 15 views
5

tôi có một vấn đề xử lý các JSON phân tích dữ liệu sử dụng AFNetworking.Parsing JSON sử dụng AFNetworking 2,0 ngoại lệ

đây là mã của tôi:

 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); 
     }]; 

khi im cố gắng để đăng nhập giá trị tôi nhận được lỗi này:

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 

gì có thể gây ra vấn đề này? responseObject là từ điển? nếu mã của tôi sai, tôi nên xử lý điều này như thế nào?

Trả lời

18

Bạn đang thiết lập các phân tích cú pháp đối phó với AFHTTPResponseSerializer và không AFJSONResponseSerializer. Vì vậy, phản hồi không bao giờ được phân tích cú pháp JSON.

Vì vậy, thay đổi

manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 

để

manager.responseSerializer = [AFJSONResponseSerializer serializer]; 

Như đã đề cập ở bạn nhận xét máy chủ được cung cấp nội dung text/html, nếu có thể các máy chủ nên nói nội dung là op loại application/json, text/json hoặc text/javascript .

Nếu nó không phải là có thể thay đổi các kiểu nội dung máy chủ, bạn có thể nói với AFJSONResponseSerializer chấp nhận text/html như:

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

2014-07-15 15: 26: 19,765 carefid [8036: 60B] Lỗi: Lỗi domain = com.alamofire.error.serialization.response Mã = -1016 "Yêu cầu thất bại: không thể chấp nhận content-type: text/html" UserInfo = 0x10d924d10 {com.alamofire.serialization.response.error.response = bây giờ tôi nhận được lỗi này .. – OshriALM

+0

máy chủ của bạn đang gửi nội dung JSON để ứng dụng là 'text/html' đó là không chính xác và nếu có thể nên được thay đổi trên máy chủ cho' application/json', 'text/json' hoặc' text/javascript' – rckoenes

+0

có cách nào để tôi có thể xử lý nó từ phía tôi không? – OshriALM

Các vấn đề liên quan