将 NSMutableArray 或 NSDictionary 发送到 php 到 mysql
Sending NSMutableArray OR NSDictionary to php to mysql
在我的应用程序中,我做了一个 activity 来从用户那里获取输入数字,每次输入数据的文本字段数量都不同,它是一个 tableView,每个单元格都有一个文本字段,我设法将所有数字存储在 NSMutableArray
中,我一直在研究如何将其传递给我的 php,我似乎找不到可以帮助我的东西,而且无法发送作为 NSString 使用 "StringWithFormat
".
要不要把NSMutableArray
改成NSDictionary
?我如何将其发送到我的 php 代码?
我正在使用 AFNetworking
来管理我的所有连接,这很好,只是在发送数组时卡住了。
我的 NSMutableArray
中的每个对象都包含
StudentName, StudentMark, StudentID
有什么建议或好的教程可以让我学习吗?
我对 IOS 和 objective-c 一无所知,所以我不知道他们如何将 post 数据发送到服务器。我所知道的平台允许发送 JSON,这就是用于将数组发送到 PHP 的平台。 PHP 然后简单地 json_decodes 将数据放入 PHP 数组中。因此,如果您使用 NSJSON 序列化(只是查了一下),您可以发送数据并在 PHP.
中处理它
AFNetworking,默认情况下,使用 AFHTTPRequestSerializer
(创建 application/x-www-form-urlencoded
请求)。这不太适合发送值数组(尤其是字典数组)。
你有几个选择,假设你有一个数组 students
:
手动使用NSJSONSerialization
将字典数组编码为字符串,然后将其作为参数传递到字典中:
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);
}];
如果您这样做,您的服务器代码将获取 $_POST['students']
,然后 json_decode
。
更改 Web 服务以接受 JSON 请求(即 application/json
请求而不是 application/x-www-form-urlencoded
请求)。
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:urlString parameters:students success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
不过,如果您这样做,则需要对您的 Web 服务进行更根本的更改以接受 JSON 请求(例如,而不是使用 $_POST
,您需要手动获取输入并JSON 解码:
<?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;
?>
第一个选项可能是最简单的,但缺乏一定的优雅(在 x-www-form-urlencoded
请求中嵌入 JSON)。后一种选择(更改 Web 服务以接受 JSON 请求)需要做更多的工作(例如,一旦您有一个使用 JSON 的 Web 服务请求,您可能希望整个 Web 服务使用 JSON 始终如一)。
在我的应用程序中,我做了一个 activity 来从用户那里获取输入数字,每次输入数据的文本字段数量都不同,它是一个 tableView,每个单元格都有一个文本字段,我设法将所有数字存储在 NSMutableArray
中,我一直在研究如何将其传递给我的 php,我似乎找不到可以帮助我的东西,而且无法发送作为 NSString 使用 "StringWithFormat
".
要不要把NSMutableArray
改成NSDictionary
?我如何将其发送到我的 php 代码?
我正在使用 AFNetworking
来管理我的所有连接,这很好,只是在发送数组时卡住了。
我的 NSMutableArray
中的每个对象都包含
StudentName, StudentMark, StudentID
有什么建议或好的教程可以让我学习吗?
我对 IOS 和 objective-c 一无所知,所以我不知道他们如何将 post 数据发送到服务器。我所知道的平台允许发送 JSON,这就是用于将数组发送到 PHP 的平台。 PHP 然后简单地 json_decodes 将数据放入 PHP 数组中。因此,如果您使用 NSJSON 序列化(只是查了一下),您可以发送数据并在 PHP.
中处理它AFNetworking,默认情况下,使用 AFHTTPRequestSerializer
(创建 application/x-www-form-urlencoded
请求)。这不太适合发送值数组(尤其是字典数组)。
你有几个选择,假设你有一个数组 students
:
手动使用
NSJSONSerialization
将字典数组编码为字符串,然后将其作为参数传递到字典中: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); }];
如果您这样做,您的服务器代码将获取
$_POST['students']
,然后json_decode
。更改 Web 服务以接受 JSON 请求(即
application/json
请求而不是application/x-www-form-urlencoded
请求)。manager.requestSerializer = [AFJSONRequestSerializer serializer]; [manager POST:urlString parameters:students success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"%@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@", error); }];
不过,如果您这样做,则需要对您的 Web 服务进行更根本的更改以接受 JSON 请求(例如,而不是使用
$_POST
,您需要手动获取输入并JSON 解码:<?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; ?>
第一个选项可能是最简单的,但缺乏一定的优雅(在 x-www-form-urlencoded
请求中嵌入 JSON)。后一种选择(更改 Web 服务以接受 JSON 请求)需要做更多的工作(例如,一旦您有一个使用 JSON 的 Web 服务请求,您可能希望整个 Web 服务使用 JSON 始终如一)。