【问题标题】:Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed using AFNetworkingError Domain=NSCocoaErrorDomain Code=3840 "使用 AFNetworking 无法完成操作
【发布时间】:2014-12-01 17:43:44
【问题描述】:

我正在使用 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.}

我不明白代码有什么问题。

【问题讨论】:

    标签: ios objective-c json afnetworking


    【解决方案1】:

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

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

    AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    

    【讨论】:

      【解决方案2】:

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

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

      试试这个:

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

      代替:

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

      【讨论】:

      • 很好的答案,显示了需要输入此代码的位置。一个小细节,但很重要
      • 我在这个问题上停留了一段时间,尝试了这个答案中的所有内容 -> stackoverflow.com/questions/19114623/… 但你的答案是有效的。
      • @iDeveloper 谢谢它对我有用...挣扎了一段时间... 1 upvote
      • @iDeveloper 请告诉我什么是 [self setResponseSerializer:responseSerializer];当我在这里出错时
      【解决方案3】:
      AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
      
      //Request Serializer
      manager.requestSerializer = [AFJSONRequestSerializer serializer];
      
      //Response Serializer
      AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
      manager.responseSerializer = responseSerializer;
      

      【讨论】:

        猜你喜欢
        • 2015-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-24
        • 2012-02-11
        • 2015-05-19
        • 1970-01-01
        • 2011-08-27
        相关资源
        最近更新 更多