AFNetworking 2.0:无法使用IOS的JSON进行POST [英] AFNetworking 2.0 : Cannot POST with JSON from IOS

查看:94
本文介绍了AFNetworking 2.0:无法使用IOS的JSON进行POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ever since upgrading my IOS code to use AFNetworking version 2.0 instead of 1.x, I cannot do an HTTP Post any more with JSON parameters.

我怀疑是因为我的HTTP请求标头不是"application/json",但我尝试这样做,但仍然无法正常工作.

I suspect it is because my HTTP request header is not "application/json" but I have tried to do that and it still does not work.

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

This is my test code for trying to POST by 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];
        });
    }];

});

而且我不断收到此错误:

and I keep getting this 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=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";

}}>

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

I am very sure that the server is up and accessible because all other GET calls work properly.

我已经检查了AFNetworking 2.0的更新文档,看来我所拥有的是执行JSON POST的正确方法

I have checked the updated documentation for AFNetworking 2.0 and it seems like what I have is the right way to do a JSON POST

请让我知道是否错过了任何事情

Please let me know if I missed anything

谢谢 是

推荐答案

实际上,我终于可以使用它了.

Actually, I finally got it to work.

基本上,对于我的AFHTTPRequestOperationManager的requestSerializer,我使用的是AFHTTPRequestSerializer,然后尝试将Content-Type设置为"application/json"

Basically, for my requestSerializer of the AFHTTPRequestOperationManager, I was using AFHTTPRequestSerializer and then trying to set the Content-Type to "application/json"

但是一旦我改用AFJSONRequestSerializer,它现在可以正常工作

But once I switched to using AFJSONRequestSerializer instead, it works fine now

AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

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

operationManagerInstance.requestSerializer = requestSerializer;

这篇关于AFNetworking 2.0:无法使用IOS的JSON进行POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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