ios 错误域=NSCocoaErrorDomain 代码=3840“无法使用 AFNetworking 完成操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26233611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed using AFNetworking
提问by user1954352
I am using AFNetworking library to post data on server using POST method.
我正在使用 AFNetworking 库使用 POST 方法在服务器上发布数据。
Following is my code
以下是我的代码
- (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);
}];
}
but I got following error in response
但我收到以下错误响应
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.}
I can't understand what is the problem with the code.
我无法理解代码有什么问题。
回答by Vlad
The problem comes from response parsing.
You are trying to de-serialize a JSON
reponse (which MUST be contained in either a NSArray
or NSDictionary
) however your response is none of the above (Most likely a simple string).
问题来自响应解析。您正在尝试反序列化一个JSON
响应(必须包含在 aNSArray
或 中NSDictionary
),但是您的响应不是上述任何一个(很可能是一个简单的字符串)。
Also, try to set the "allow fragments" to the response serializer.
此外,尝试将“允许片段”设置为响应序列化程序。
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
回答by pankaj asudani
May be you need authentication to access JSON
response. Set authentication like that:
可能是您需要身份验证才能访问JSON
响应。像这样设置身份验证:
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"XYZ" password:@"xyzzzz"];
Try this:
尝试这个:
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[self setResponseSerializer:responseSerializer];
instead of:
代替:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
回答by Pranit
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
//Request Serializer
manager.requestSerializer = [AFJSONRequestSerializer serializer];
//Response Serializer
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer = responseSerializer;