2014-10-07 83 views
21

我使用AFNetworking庫使用POST方法來發布服務器上的數據完成。錯誤域= NSCocoaErrorDomain代碼= 3840「的操作不能使用AFNetworking

以下是我的代碼

- (void) callLoginAPI:(NSDictionary *)dictProfile{ 
    // 1 
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:[dictProfile valueForKey:@"name"], @"username", 
                    [dictProfile valueForKey:@"first_name"],@"first_name", 
                    [dictProfile valueForKey:@"last_name"],@"last_name", 
                    [dictProfile valueForKey:@"email"],@"email", 
                    [dictProfile valueForKey:@"birthday"],@"dob", 
                    [dictProfile valueForKey:@"gender"],@"gender", 
                    [[dictProfile valueForKey:@"location"] valueForKey:@"name"],@"location", 
                    [dictProfile valueForKey:@"timezone"],@"timezone", 
                    @"",@"language", 
                    [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=large",[dictProfile valueForKey:@"id"]],@"profile_pic_url", 
                    @"",@"cover_pic_url",nil]; 



    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    manager.requestSerializer = [AFJSONRequestSerializer serializer]; 



    [manager POST:@"http://10.1.81.35:8000/api/login/" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"JSON: %@", responseObject); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", 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=0x797f2620 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} 

我不明白什麼是代碼中的問題下面的錯誤。

回答

42

問題來自響應分析。 您正在嘗試反序列化一個JSON響應(它必須包含在NSArrayNSDictionary中),但是您的響應不屬於上述情況(很可能是簡單的字符串)。

此外,嘗試設置「允許片段」到響應串行器。

AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments]; 
+2

生活保護隊友,感謝 – cesarferreira 2014-12-16 12:05:29

+4

它不是爲我工作 – 2015-03-18 09:27:40

+0

不是爲我工作了。 – 2016-09-22 11:13:36

14

可能是您需要身份驗證才能訪問JSON響應。這樣的設置身份驗證:

[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"XYZ" password:@"xyzzzz"]; 

試試這個:而不是

AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments]; 
[self setResponseSerializer:responseSerializer]; 

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
manager.requestSerializer = [AFJSONRequestSerializer serializer]; 
+0

很好的答案,顯示了這段代碼需要輸入的地方。一個小細節,但非常重要 – 2015-06-01 11:49:38

+0

我被困在這個問題一段時間,嘗試在這個答案的一切 - > http://stackoverflow.com/questions/19114623/request-failed-unacceptable-content-type-text-html-使用網絡-2-0,但你的答案是有效的。 – Allen 2016-04-22 17:09:37

+0

@iDeveloper感謝它爲我工作...掙扎了一段時間... 1 upvote – 2017-02-16 09:44:27

相關問題