AFNetworking 2.0:无法使用IOS的JSON进行POST

 由来只有新人笑_谁能记得旧人哭 发布于 2023-02-12 19:15

自从升级我的IOS代码以使用AFNetworking版本2.0而不是1.x后,我无法再使用JSON参数执行HTTP Post.

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

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

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:  { URL: http://1.2.3.4:9000/api/login }, response:  { URL: http://1.2.3.4:9000/error404 } { status code: 404, headers {
"Content-Length" = 1809;
"Content-Type" = "text/html; charset=utf-8";

}}>

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

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

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

谢谢你

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

    现在它会起作用.

    2023-02-12 19:16 回答
  • 实际上,我终于开始工作了.

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

    但是一旦我转而使用AFJSONRequestSerializer,它现在工作正常

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

    2023-02-12 19:16 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有