How To POST JSON Parameters Using AFNetworking?
Asked Answered
I

2

5

how can I POST JSON parameters with AFNetworking? Below is the example JSON format. Thank you in advance.

{
    "application": {
        "identifier": "appDev"
    },
    "sso": {
        "version": 2.0,
        "session_expiry": 43200
    },
    "user": {
        "password": "xxxx~!@",
        "username": "xxx0min"
    },
    "coordinate": {
        "latitude": 10.9948629,
        "longitude": -169.5213749
    }
}

I successfully created a JSON :

NSDictionary *parameters = @{@"application": @{@"identifier": @"appDev"}, @"sso": @{@"version": @"2.0", @"session_expiry":@43200}, @"user": @{@"password":@"xxxx~!@", @"username":@"xxx0min"}, @"coordinate": @{@"latitude":@10.9948629, @"longitude":@-169.5213749}};
Iconic answered 23/12, 2015 at 11:30 Comment(6)
Convert that into a NSDictionary and post it as a parameter, #21487684Perusse
#18342571Dogmatics
By default, the requestSerializer is [AFHTTPRequestSerializer serializer]. If you server is designed to receive JSON requests, you want manager.requestSerializer = [AFJSONRequestSerializer serializer].Oxley
This is what I do. But it doesn't work. NSDictionary *parameters = @{@"identifier": @"calendar-mobile", @"version": @"2.0", @"session_expiry": @43200, @"password": @"xxxx~!@", @"username":@"xxx0min", @"latitude":@"10.9948629", @"longitude":@"-169.5213749"};Iconic
That dictionary doesn't match the JSON you've listed in your question, for example @"identifier": @"calendar-mobile" should be in a dictionary that is @{@"application" : @{@"identifier": @"calendar-mobile"}} - if you post your code in the question rather than just the JSON people might be able to helpPerusse
thanks @Flexicoder, finally my JSON matched with my question. It's silly mistake actually that I didn't noticed.Iconic
L
9

First convert JSON string to NSDictionary:

NSError *error;
NSData *objectData = [@"{\"1\":\"2\"}" dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                      options:NSJSONReadingMutableContainers 
                                        error:&error];

Then you can send this dictionary with AFNetworking. In version 3 you should use AFHTTPSessionManager:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

[manager POST:@"your_URL" parameters:json progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@"Complete");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"Fail");
}];
Leonaleonanie answered 23/12, 2015 at 12:3 Comment(1)
thank you for answering. thumbs up for AFNetworking version 3.Iconic
I
3

Finally I found a solution to my problem. What I was doing is making NSDictionary of NSDictionary for my JSON parameters. Below is the full solution code.

NSString *loginURL = @"http://myappdev.dev/login";

 NSDictionary *parameters = @{@"application": @{@"identifier": @"appDev"}, @"sso": @{@"version": @"2.0", @"session_expiry":@43200}, @"user": @{@"password":@"xxxx~!@", @"username":@"xxx0min"}, @"coordinate": @{@"latitude":@10.9948629, @"longitude":@-169.5213749}};

    NSLog(@"params : %@", parameters);

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager POST:loginURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON Successsss: %@", responseObject);
        NSLog(@"operation Successsss: %@", operation);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error laaa: %@", error);
    }];
Iconic answered 23/12, 2015 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.