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}};
Mohamad Afiq
  • 275
  • 1
  • 5
  • 14
  • 3
    Convert that into a NSDictionary and post it as a parameter, http://stackoverflow.com/questions/21487184/posting-json-data-using-afnetworking-2-0 – Flexicoder Dec 23 '15 at 11:32
  • http://stackoverflow.com/questions/18342071/afnetworking-send-array-in-json-parameters-of-post-request – Rahul Patel Dec 23 '15 at 11:34
  • 2
    By default, the `requestSerializer` is `[AFHTTPRequestSerializer serializer]`. If you server is designed to receive JSON requests, you want `manager.requestSerializer = [AFJSONRequestSerializer serializer]`. – Rob Dec 23 '15 at 11:35
  • 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"}; – Mohamad Afiq Dec 23 '15 at 11:39
  • 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 help – Flexicoder Dec 23 '15 at 11:49
  • thanks @Flexicoder, finally my JSON matched with my question. It's silly mistake actually that I didn't noticed. – Mohamad Afiq Dec 23 '15 at 11:59

2 Answers2

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");
}];
Rost
  • 265
  • 1
  • 11
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);
    }];
Mohamad Afiq
  • 275
  • 1
  • 5
  • 14