AFNetworking 2.0:无法从IOS使用JSON发布

AFNetworking 2.0:无法从IOS使用JSON发布,ios,json,post,afnetworking,afnetworking-2,Ios,Json,Post,Afnetworking,Afnetworking 2,自从将IOS代码升级为使用AFNetworking 2.0版而不是1.x版后,我就无法再使用JSON参数进行HTTP Post了 我怀疑这是因为我的HTTP请求头不是“application/json”,但我已经尝试过这样做,但仍然不起作用 这是我尝试通过JSON发布的测试代码: NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys: email,

自从将IOS代码升级为使用AFNetworking 2.0版而不是1.x版后,我就无法再使用JSON参数进行HTTP Post了

我怀疑这是因为我的HTTP请求头不是“application/json”,但我已经尝试过这样做,但仍然不起作用

这是我尝试通过JSON发布的测试代码:

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                email, @"email",
                                password, @"password",
                                nil];

[[OTApiClient sharedInstance] POST:@"login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"result: %@", responseObject);

        Boolean response = [[responseObject valueForKey:@"success"] boolValue];
        if(response == TRUE) {
            //ok, success
            NSLog(@"success");

            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Successfully logged in" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];


        } else {
            NSLog(@"Not successful");

            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"The email or password is incorrect." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];
        }


        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        });


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@ ,  for operation: %@", error, operation);


        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"There is a problem connecting to the server, please try again soon." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];


        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        });
    }];

});
我一直在犯这样的错误:

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=0xca80730 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} ,  for operation: <AFHTTPRequestOperation: 0xca6bee0, state: isFinished, cancelled: NO request: <NSMutableURLRequest: 0xca76040> { URL: http://1.2.3.4:9000/api/login }, response: <NSHTTPURLResponse: 0xc963d60> { URL: http://1.2.3.4:9000/error404 } { status code: 404, headers {
"Content-Length" = 1809;
"Content-Type" = "text/html; charset=utf-8";
Error Domain=NSCocoaErrorDomain code=3840“操作无法完成。(Cocoa错误3840)。“(JSON文本未以数组或对象开头,允许未设置片段的选项。)UserInfo=0xca80730{NSDebugDescription=JSON文本未以数组或对象开头,允许未设置片段的选项。},对于操作:

我非常确定服务器已经启动并且可以访问,因为所有其他GET调用都正常工作

我已经查看了AFNetworking 2.0的更新文档,似乎我所拥有的是做JSON文章的正确方法

如果我遗漏了什么,请告诉我

谢谢
实际上,我终于让它工作了

基本上,对于我的AFHTTPRequestOperationManager的requestSerializer,我使用AFHTTPRequestSerializer,然后尝试将内容类型设置为“application/json”

但一旦我转而使用AFJSONRequestSerializer,它现在就可以正常工作了

AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

operationManagerInstance.requestSerializer = requestSerializer;

我通过Swift使用AFNetworking API时遇到了同样的问题,尝试发送post请求,最后由于user1805458的post,它也能正常工作

基本上,对于我的AFHTTPRequestOperationManager的requestSerializer,我也使用了AFHTTPRequestSerializer,然后尝试将内容类型设置为“Application/JSON”,但它不起作用

但一旦我转而使用AFJSONRequestSerializer,它现在就可以正常工作了:

        let manager = AFHTTPRequestOperationManager()
        manager.requestSerializer = AFJSONRequestSerializer(writingOptions: nil)

        // Contrary to user1805458's solution, I did not need to use these two instructions below using Swift.
        // manager.requestSerializer.setValue("Application/JSON", forHTTPHeaderField:"Content-Type")
        // manager.requestSerializer.setValue("Application/JSON", forHTTPHeaderField:"Accept")
如果您使用Swift、AFNetworking并尝试发送包含JSON正文的post请求,我希望它将帮助您修复此错误

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = serializer;

现在它可以工作了。

你能发布你的JSON吗。错误消息中返回的JSON内容不是有效的JSON,因此我想知道这是否是问题所在;而不是application/json?您是否尝试在服务器端修复此问题?尝试创建一个操作并添加操作。JSONReadingOptions=NSJSONReadingAllowFragments;嗨,格泽戈兹,我会试试JSONReadingOptions,然后告诉你。另外,我相信文本/html是由于服务器端的404页面而生成的。对于mttdbrd,我在上面的示例中发布的JSON是{“email”:“some email”,“password”:“some password”},我假设它没有问题,因为它是由AFNetworking在给定NSDictionary参数的情况下自动生成的。工作完美。我已经浪费了将近5个小时来调试和检查问题。