2013-11-01 68 views
4

我調用此函數並返回Cocoa錯誤3840每次。 我嘗試調試並修復它,它,而不是當分析結果可可錯誤3840 POST請求與AFNetworking 2


錯誤時,請求我發現這個錯誤的故障塊時請求。

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=0x109230960 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not et.} 
2013-11-01 12:09:30.925 MagicBox[87431:70b] The operation couldn’t be completed. (Cocoa error 3840.) 

這是我的代碼 在此先感謝。

- (void)loginWithUserName:(NSString *)userName 
      Password:(NSString *)password 
      orFacebook:(NSString *)facebookID 
    withResponseBlock:(ResponseBlock)responseBlock { 

if (!userName && !facebookID) { 
    NSError *error = [NSError errorWithDomain:@"Missing Parameters" 
             code:400 
            userInfo:@{ NSLocalizedDescriptionKey : @"Username or FacebookID is required"}]; 
    responseBlock(error, nil); 
} 

NSDictionary *params; 

if (facebookID) { 
    params = @{ @"fb_id": facebookID }; 
} else { 
    params = @{ @"username": userName, 
       @"password": password }; 
} 

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
NSString *loginURL = [self requestWithPath:@"/api/login"]; 
[manager POST:loginURL 
    parameters:params 
constructingBodyWithBlock:nil 
     success:^(AFHTTPRequestOperation *operation, id responseObject) { 
      if (responseBlock) { 
       responseBlock(nil, responseObject); 
      } 
     } 
     failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      if (responseBlock) { 
       responseBlock(error, nil); 
      } 
     }]; 
} 
+0

見http://stackoverflow.com/a/19692152/1445366 –

+0

謝謝大家,這錯誤代碼的含義JSONSerializer無法解析,因爲AFNetworking2默認響應序列化到JSON我的API響應和問題是我的API返回的字符串 –

回答

4

已經陷入了同樣的問題。

我解決了它本:

self.requestSerializer = [AFJSONRequestSerializer serializer]; 

即雙重檢查AFHTTPRequestOperationManager是否使用正確的請求序列化。

希望這能幫助你!

3

我通過確保返回的數據與谷歌電子表格XML響應(而不是JSON)

manager.responseSerializer = [AFXMLParserResponseSerializer serializer]; 
2

正確的系列化我加入的代碼

經理以下行解決了這個問題解決了這一點。 requestSerializer = [AFJSONRequestSerializer serializer];

16

我解決它通過添加代碼下面的行

manager.responseSerializer = [AFHTTPResponseSerializer串行];

+0

解決我的的問題,謝謝。 – Leonly91