I am working on an iOS json client for a php webservice and for some reason I’m having problems parsing a fairly simple json response from the server. What do I need to do?
Edit
The problem isn’t the parsing so it must be the jsonString I’m sending the webservice.
Basically I need to send this
POST values
app = mark
data = { “01,E1,AT333AT333,9053839719,2012-08-28 14:35:58,2012-08-28 14:35:58,43.154650,-79.3877390,1000,YS3DD55H812035739,1000000,3434” }
The current code is printing null for both strings.
Here’s my method that handles the call to the webservice.
- (IBAction)submitMessage
{
NSString* apps =@"mark";
NSString* data = @"01,E1,AT333AT333,9053839719,2012-08-28 14:35:58,2012-08-28 14:35:58,43.154650,-79.3877390,1000,YS3DD55H812035739,1000000,3434";
NSDictionary* jsonDictionary=[NSDictionary dictionaryWithObject: data forKey:@"data"];
NSString* jsonString = [jsonDictionary JSONRepresentation];
self.outPut.text=jsonString;
NSLog(@"Made it");
NSURL *url = [NSURL URLWithString:@"http://secure.g4apps.com/g4webservices.php"];
AFHTTPClient *httpClient=[[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params =[NSDictionary dictionaryWithObjectsAndKeys:
apps,@"app",
jsonString,@"smpdata",nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"g4webservices.php" parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Response: %@ %@", [JSON valueForKeyPath:@"Status"], [JSON valueForKeyPath:@"Data"]);
self.outPut2.text = [NSString stringWithFormat:@"response: %@ %@",[JSON valueForKeyPath:@"Status"], [JSON valueForKeyPath:@"Data"]]; }
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error,id JSON){
NSLog(@"[Error]: %@", error);
}
];
[operation start];
}
Here’s a sample json response.
{
"Status":"11",
"Data":["AT333AT333,43.1547986,-79.3884892",
"BT343BT343,43.1547986,-79.3884892"]
}
The problem was the JSON string I was sending. The service was not using key value pairs for the json string it was receiving and I was unable to figure out how to reproduce that in Objective-C.
Post Variable where as follows
app=”app_name”
data={ “Some csv data” }
What I was able to get Objective C to produce was
app=”app_name”
data={ “Data”:{“Some csv data”} }
So I modified the server to handle what iOS produced and now everythings working. Figure out how to handle the response when the Data array has more than one value.