将NSMutableArray或NSDictionary发送到php到mysql
在我的应用程序中,我做了一个从用户那里获取输入数字的活动,输入数据的文本字段的数量每次都不同,它是一个tableView,每个单元格都有一个文本字段,我设法将所有数字存储在将NSMutableArray或NSDictionary发送到php到mysql,php,ios,mysql,objective-c,nsmutablearray,Php,Ios,Mysql,Objective C,Nsmutablearray,在我的应用程序中,我做了一个从用户那里获取输入数字的活动,输入数据的文本字段的数量每次都不同,它是一个tableView,每个单元格都有一个文本字段,我设法将所有数字存储在NSMutableArray中,我一直在研究如何将其传递到我的php,我似乎找不到帮助我的东西,并且无法使用“StringWithFormat”将其作为NSString发送 是否应将NSMutableArray更改为NSDictionary?如何将其发送到我的php代码 我正在使用AFNetworking管理我的所有连接,这很
NSMutableArray
中,我一直在研究如何将其传递到我的php,我似乎找不到帮助我的东西,并且无法使用“StringWithFormat
”将其作为NSString发送
是否应将NSMutableArray
更改为NSDictionary
?如何将其发送到我的php代码
我正在使用AFNetworking
管理我的所有连接,这很好,只是在发送阵列时遇到了问题
myNSMutableArray
中的每个对象包括
StudentName, StudentMark, StudentID
有什么建议或好的教程可以学习吗?我对IOS和objective-c一无所知,所以我不知道它们是如何将post数据发送到服务器的。我所知道的平台允许发送JSON,这就是将数组发送到PHP的方式。PHP然后将数据简单地输入到PHP数组中。因此,如果您使用NSJSONSerialization(只是查找一下),您可以发送数据并用PHP进行处理。默认情况下,AFNetworking使用
AFHTTPRequestSerializer
(它创建应用程序/x-www-form-urlencoded
请求)。这不适合发送值数组(尤指字典数组)
假设你有一组学生,你有两个选择:
手动使用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);
}];
如果您这样做,您的服务器代码将获取$\u POST['students']
,然后对其进行解码
更改web服务以接受JSON请求(即应用程序/JSON
请求,而不是应用程序/x-www-form-urlencoded
请求)
但是,如果您这样做,则需要对web服务进行更根本的更改,以接受JSON请求(例如,不要使用$\u 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)。谢谢您的帮助,我正在努力让它工作,我在这里发布了我尝试的内容,如果您愿意看一看的话
<?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;
?>