In my application, i made an activity to get input numbers from the user, the number of textfields for input data is different each time, it's a tableView and each cell has a textfield, i managed to store all the numbers in a NSMutableArray
, and I've been looking into how to pass it to my php, I can't seem to find something to help me, and it can't be send as NSString using "StringWithFormat
".
Should NSMutableArray
Be changed into NSDictionary
? How Do i Send it to my php code ?
I am using AFNetworking
to manage all my connections and it's great, just got stuck on sending an array.
each object in my NSMutableArray
consists of
StudentName, StudentMark, StudentID
any suggestions or good tutorials i can learn from ?
AFNetworking, by default, uses the AFHTTPRequestSerializer
(which creates application/x-www-form-urlencoded
requests). This is not well suited for sending an array of values (esp an array of dictionaries).
You have a couple of options, assuming you have an array of students
:
Manually use NSJSONSerialization
to encode the array of dictionaries into a string, and then pass that as a parameter in a dictionary:
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:students options:0 error:&error];
NSAssert(jsonData, @"Problem encoding JSON: %@", error);
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[manager POST:urlString parameters:@{@"students": jsonString} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
If you do that, your server code would grab $_POST['students']
, and then json_decode
that.
Change the web service to accept JSON requests (i.e. an application/json
request rather than an application/x-www-form-urlencoded
requests).
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:urlString parameters:students success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
If you do that, though, it requires a more fundamental change in your web service to accept JSON requests (e.g., rather than using $_POST
, you'd manually grab the input and JSON decode it:
<?php
// read the raw post data
$handle = fopen("php://input", "rb");
$raw_post_data = '';
while (!feof($handle)) {
$raw_post_data .= fread($handle, 8192);
}
fclose($handle);
// decode the JSON into an associative array
$request = json_decode($raw_post_data, true);
// you can now access the associative array, $request,
// e.g. save it to your database
// let's assume you built a `$response` dictionary summarizing
// the success or failure of the above code, now let's output it:
$raw_response = json_encode($response);
// specify headers
header("Content-Type: application/json");
header("Content-Length: " . strlen($raw_response));
// output response
echo $raw_response;
?>
The first option is probably easiest, but lacks a certain elegance (embedding JSON within a x-www-form-urlencoded
request). The latter option (changing web service to accept JSON requests) would require a little more work, though (e.g. once you have one web service request using JSON, you might want the whole web service to use JSON consistently).
I don't know anything about IOS and objective-c so I don't know how they send post data to a server. The platforms that I know about allow the sending of JSON and that is what would be used to send an array to PHP. PHP then simply json_decodes the data into a PHP array. So, if you use NSJSONSerialization (just looked that up) you can send your data and process it in PHP.