使用AFNetworking 2.0发布JSON数据

时间:2014-01-31 18:15:33

标签: ios json http-post afnetworking-2

我有一个Web脚本,它通过HTTP POST请求接受JSON字符串作为输入。 我遇到过几个相同的AFNetworking 1.x示例,有人可以指点我或给AFNetworking 2.0示例对带有格式化JSON作为输入的Web脚本执行HTTP POST请求吗?

由于

5 个答案:

答案 0 :(得分:78)

搜索文档并尝试一些代码后,我作为示例

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


NSDictionary *params = @ {@"user" :txtUserName, @"pwd" :txtPwd };


[manager POST:URL_SIGNIN parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
    NSLog(@"JSON: %@", responseObject);
}
failure:
 ^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Error: %@", error);
 }];

另外,不要忘记在服务器脚本中将响应头类型设置为Application / json。

答案 1 :(得分:20)

以下是以JSON格式存储在NSMutableDictionary参数中的POST参数的一些简单模板。适用于AFNetworking 2.4.1。

NSString *baseURL = @"http://your-server.com/";
NSString *path = @"method/url/";

NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:@"value" forKey:@"key"];

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager POST:path parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {

        NSLog(@"JSON: %@", responseObject);
        //here is place for code executed in success case

} failure:^(NSURLSessionDataTask *task, NSError *error) {

        //here is place for code executed in error case
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error while sending POST"
                                                            message:@"Sorry, try again."
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];

        NSLog(@"Error: %@", [error localizedDescription]);
}];

答案 2 :(得分:8)

如果您想将json发布到服务器,您应该使用以下方式发布您的参数。

使用这种方式,您可以找到" Content-Type"在您的请求标题中是" application / json"。

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

 NSDictionary *paras = @{@"uid" : @(10020)};

[manager POST:@"http://your.request.url" parameters:paras success:^(AFHTTPRequestOperation *operation, id responseObject) {



}failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"the falire is %@", error);

}];

愿这有帮助。 :)

答案 3 :(得分:5)

在swift:

manager.AFHTTPRequestOperationManager

中添加此内容
manager.requestSerializer = AFJSONRequestSerializer(writingOptions: NSJSONWritingOptions.PrettyPrinted)

答案 4 :(得分:1)

如果你想发布参数字典json字符串使用以下代码

NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"your_webservice_post_url"]];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSError *error;
NSDictionary *parameters = @{@"customValue":@"value"};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&error];
 [urlRequest setHTTPMethod:@"POST"];
 [urlRequest setHTTPBody:jsonData];