错误域= NSCocoaErrorDomain代码= 3840“无法使用AFNetworking完成操作

我正在使用AFNetworking库使用POST方法在服务器上发布数据。

以下是我的代码

- (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.} 

我无法理解代码有什么问题。

问题来自响应解析。 您正在尝试反序列化JSON响应(必须包含在NSArrayNSDictionary ),但您的响应不是上述(很可能是一个简单的字符串)。

此外,尝试将“允许片段”设置为响应序列化程序。

 AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments]; 

您可能需要身份validation才能访问JSON响应。 像这样设置身份validation:

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

尝试这个:

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

代替:

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

使用URLSession调用Web服务

Error Domain = NSCocoaErrorDomain Code = 3840“操作无法完成。(Cocoa error 3840.)”(JSON文本不是以数组或对象开头,而是选项允许未设置片段。)UserInfo = 0x8a8a700 {NSDebugDescription = JSON text没有从数组或对象和选项开始,以允许片段未设置。}

解决方案是

 URLSession.shared.dataTask(with: url) { (data, response, error) in if let jsonData = data { do { let parsedData = try JSONSerialization.jsonObject(with: jsonData, options: .mutableLeaves) as! [String: AnyObject] } catch let err{ print("\n\n===========Error===========") print("Error Code: \(error!._code)") print("Error Messsage: \(error!.localizedDescription)") if let data = data, let str = String(data: data, encoding: String.Encoding.utf8){ //Print server response data print("Server Error: " + str) } debugPrint(error) print("===========================\n\n") debugPrint(err) } } else { debugPrint(error as Any) } }.resume() 
 AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; //Request Serializer manager.requestSerializer = [AFJSONRequestSerializer serializer]; //Response Serializer AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments]; manager.responseSerializer = responseSerializer; 
Interesting Posts