Error Domain = NSCocoaErrorDomain Code = 3840"使用AFNetworking无法完成操作 [英] Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed using AFNetworking

查看:678
本文介绍了Error Domain = NSCocoaErrorDomain Code = 3840"使用AFNetworking无法完成操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using AFNetworking library to post data on server using POST method.

以下是我的代码

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

推荐答案

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

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).

另外,尝试将允许片段设置为响应序列化器。

Also, try to set the "allow fragments" to the response serializer.

AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];

这篇关于Error Domain = NSCocoaErrorDomain Code = 3840"使用AFNetworking无法完成操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆