-1

I'm working on a mobile app that takes personal information from users then saves it to the php server. I'm having a problem on the data with array of dictionaries, how do I fix this?

The sample data that the mobile app sends to the server looks like this, see the work_experience, it's an array of dictionaries:

sample data

Don't mind the data values, it's taken on different times, mind the data structure in work_experience

It becomes like this when it reaches the server:

server received data

This is how the work_experience gets saved, which is wrong:

database

This is my post request:

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

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

php function that receives the post request:

$params = $this->params()->fromPost();

$userId = $this->getUsersTable()->saveUserInfo($params);

$this->getSkillsTable()->saveSkills($params['skillset'], $userId);

$this->getWorkExperienceTable()->saveWorkExperience($params['work_experience'], $userId);

$view = new JsonModel($params);

return $view;
Bryan P
  • 4,142
  • 5
  • 41
  • 60
  • 1
    Neither of the data screen-shots look like json to me. – jeroen Jun 18 '14 at 02:25
  • @jereon not valid, at least. – Ohgodwhy Jun 18 '14 at 02:27
  • It's just the value of an NSDictionary, but it gets converted by afnetworking post request, so think of it as the raw data, I just screened shot it out of the xcode before getting sent as parameter to the post request – Bryan P Jun 18 '14 at 02:28
  • So where do the json and php come in? – jeroen Jun 18 '14 at 02:28
  • Because php receives the data to save it to the database, so I need to know if there is something I need to do in php to correct this, so php is included, and the data becomes json in php, because arrays can't be passed to post request, and so it has to be json – Bryan P Jun 18 '14 at 02:31
  • Well, there is no json nor php in your question so that would be pretty hard to help with :-) – jeroen Jun 18 '14 at 02:32
  • Well it's a little hard to explain this, but in summary, the first picture is what I expect it to be when it reaches the server, I can't see how it looks like as a json but the afnetworking post request serializes it. The second picture is what it becomes when it reached the server, and when it gets inserted to the database as shown in picture 3, you can see that the data did change structure and does look like picture 2 which is wrong – Bryan P Jun 18 '14 at 02:42
  • possible duplicate of [Posting JSON data using AFNetworking 2.0](http://stackoverflow.com/questions/21487184/posting-json-data-using-afnetworking-2-0) - You need to set your request serializer to `[AFJSONRequestSerializer serializer]` otherwise you will get the default HTTP serializer – Paulw11 Jun 18 '14 at 03:01
  • `that by default AFHTTPRequestOperationManager sets AFJSONRequestSerializer as request serializer, so by default parameters are now encoded as json.` That is according to this http://gavrix.wordpress.com/2013/10/16/migrating-from-afnetworking-1-x-to-afnetworking-2/ thus, it's redundant to add that, and if you will look carefully, you will notice that I got my code from that post, which means that I already tried that. – Bryan P Jun 18 '14 at 03:06
  • The current source on Github https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFHTTPRequestOperationManager.m contains the line `self.requestSerializer = [AFHTTPRequestSerializer serializer];` in `initWithBaseURL` and your data in picture 2 isn't JSON encoded, so I am guessing the blog post is wrong – Paulw11 Jun 18 '14 at 03:17
  • Note the comment at the bottom of the blog post stating that AFHTTPRequestSerializer is the default – Paulw11 Jun 18 '14 at 03:23

1 Answers1

0
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

//manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
manager.requestSerializer = [AFJSONRequestSerializer serializer];

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

You are limiting the app to only accept responses with header text/html, which is not correct because you are looking for json response. And you need to set the requestSerializer to a AFJSONRequestSerializer instance because the default is AFHTTPRequestSerializer

  • This causes the data to become empty – Bryan P Jun 18 '14 at 03:56
  • 1
    You aren't setting a text/json response header in your PHP, so this isn't necessary. You will receive an error from AFNetworking if the response header doesn't match the acceptable content types, so what you had was correct - setting the correct requestSerializer will help though, so this answer is half right – Paulw11 Jun 18 '14 at 03:59
  • The JsonModel makes the response to have a content-type of application/json – user3404668 Jun 18 '14 at 05:36