iOS - 如何将数组作为参数传递给 AFNetworking (Objective-C)
iOS - How to pass array to AFNetworking as parameters (Objective-C)
我有一个 php 文件,我想用它来处理数据。 php 文件包含一个将数组作为参数的函数。
php 函数:
func((array)$input->foos as &$foo){
$request = mb_strtolower($foo->request);
$found = false;
$loopCount = 0;
while($found === false && $loopCount < 3){
if($request === 'single' || $request === 'corner'){
$foo->request = $single_priority[$loopCount];
}
else if($request === 'double'){
$foo->request = $double_priority[$loopCount];
}
else if($request === 'quad'){
$foo->request = $quad_priority[$loopCount];
}
$rooms = getRooms($foo, $link, $roomcounts[$foo->request]);
if($room = $rooms->fetch_assoc()){
$found = true;
if ($loopCount === 0){
if(!$link->query("REPLACE INTO reservations VALUES('{$room['room_id']}', '{$foo->user_id}',
'{$foo->move_in_date}', '{$foo->move_out_date}', 'Incoming', '{$foo->date_added}')")){
$arr = array();
$arr['error'] = "Error: " . $link->error;
echo json_encode($arr);
die();
}
array_push($result, array('user_id' => $foo->user_id, 'room_assigned' => $room['room_id'], 'request_status' => 0));
if(!$link->query("DELETE FROM res_waiting WHERE user_id = '{$foo->user_id}'")){
$arr = array();
$arr['error'] = "Error: " . $link->error;
echo json_encode($arr);
die();
}
}
else{
if(!$link->query("INSERT IGNORE INTO reservations VALUES('{$room['room_id']}', '{$foo->user_id}',
'{$foo->move_in_date}', '{$foo->move_out_date}', 'Incoming', '{$foo->date_added}')")){
$arr = array();
$arr['error'] = "Error: " . $link->error;
echo json_encode($arr);
die();
}
array_push($result, array('user_id' => $foo->user_id, 'room_assigned' => $room['room_id'], 'request_status' => 1));
}
}
$loopCount += 1;
}
if (found === false){
array_push($result, array('user_id' => $foo->user_id, 'room_assigned' => 0, 'request_status' => 2));
}
}
我正在使用 AFNetworking 将 php 代码连接到 Objective-C
Objective-C代码:
-(void) connectToPHP:(NSArray *)parameters
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSString *URLString = [[NSString alloc] initWithFormat:@"ip.address/email.php"];
[manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSData *jsonData = responseObject;
NSError * error=nil;
id reponse = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"JSON response: %@", [reponse objectForKey:@"response"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if(error){
NSLog(@"%@ Error:", error);
}
}];
}
函数调用:
NSDictionary *foo = @{
@"id" : @"2011011",
@"request" : @"single",
@"gender" : @"M",
@"username" : @"name",
};
NSArray *arr = [NSArray arrayWithObjects:foo, nil];
[self connectToPHP:arr];
然而,当我 运行 这个 php 文件中的函数没有正确 运行 时,我得到了一个错误。我认为这与我如何将数组作为参数传递有关。
你必须将数组路径作为参数 @{@"data": @[params]}
NSDictionary *foo = @{
@"id" : @"2011011",
@"request" : @"single",
@"gender" : @"M",
@"username" : @"name",
};
NSArray *arr = [NSArray arrayWithObjects:foo, nil];
// convert your object to JSOn String
NSError *error = nil;
NSString *createJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:arr
options:NSJSONWritingPrettyPrinted
error:&error]
encoding:NSUTF8StringEncoding];
NSDictionary *pardsams = @{@"data": createJSON};
// finally start your request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSLog(@"Dict %@",pardsams);
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:@"ip.address/email.php" parameters:pardsams success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
我有一个 php 文件,我想用它来处理数据。 php 文件包含一个将数组作为参数的函数。
php 函数:
func((array)$input->foos as &$foo){
$request = mb_strtolower($foo->request);
$found = false;
$loopCount = 0;
while($found === false && $loopCount < 3){
if($request === 'single' || $request === 'corner'){
$foo->request = $single_priority[$loopCount];
}
else if($request === 'double'){
$foo->request = $double_priority[$loopCount];
}
else if($request === 'quad'){
$foo->request = $quad_priority[$loopCount];
}
$rooms = getRooms($foo, $link, $roomcounts[$foo->request]);
if($room = $rooms->fetch_assoc()){
$found = true;
if ($loopCount === 0){
if(!$link->query("REPLACE INTO reservations VALUES('{$room['room_id']}', '{$foo->user_id}',
'{$foo->move_in_date}', '{$foo->move_out_date}', 'Incoming', '{$foo->date_added}')")){
$arr = array();
$arr['error'] = "Error: " . $link->error;
echo json_encode($arr);
die();
}
array_push($result, array('user_id' => $foo->user_id, 'room_assigned' => $room['room_id'], 'request_status' => 0));
if(!$link->query("DELETE FROM res_waiting WHERE user_id = '{$foo->user_id}'")){
$arr = array();
$arr['error'] = "Error: " . $link->error;
echo json_encode($arr);
die();
}
}
else{
if(!$link->query("INSERT IGNORE INTO reservations VALUES('{$room['room_id']}', '{$foo->user_id}',
'{$foo->move_in_date}', '{$foo->move_out_date}', 'Incoming', '{$foo->date_added}')")){
$arr = array();
$arr['error'] = "Error: " . $link->error;
echo json_encode($arr);
die();
}
array_push($result, array('user_id' => $foo->user_id, 'room_assigned' => $room['room_id'], 'request_status' => 1));
}
}
$loopCount += 1;
}
if (found === false){
array_push($result, array('user_id' => $foo->user_id, 'room_assigned' => 0, 'request_status' => 2));
}
}
我正在使用 AFNetworking 将 php 代码连接到 Objective-C
Objective-C代码:
-(void) connectToPHP:(NSArray *)parameters
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSString *URLString = [[NSString alloc] initWithFormat:@"ip.address/email.php"];
[manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSData *jsonData = responseObject;
NSError * error=nil;
id reponse = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"JSON response: %@", [reponse objectForKey:@"response"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if(error){
NSLog(@"%@ Error:", error);
}
}];
}
函数调用:
NSDictionary *foo = @{
@"id" : @"2011011",
@"request" : @"single",
@"gender" : @"M",
@"username" : @"name",
};
NSArray *arr = [NSArray arrayWithObjects:foo, nil];
[self connectToPHP:arr];
然而,当我 运行 这个 php 文件中的函数没有正确 运行 时,我得到了一个错误。我认为这与我如何将数组作为参数传递有关。
你必须将数组路径作为参数 @{@"data": @[params]}
NSDictionary *foo = @{
@"id" : @"2011011",
@"request" : @"single",
@"gender" : @"M",
@"username" : @"name",
};
NSArray *arr = [NSArray arrayWithObjects:foo, nil];
// convert your object to JSOn String
NSError *error = nil;
NSString *createJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:arr
options:NSJSONWritingPrettyPrinted
error:&error]
encoding:NSUTF8StringEncoding];
NSDictionary *pardsams = @{@"data": createJSON};
// finally start your request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSLog(@"Dict %@",pardsams);
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:@"ip.address/email.php" parameters:pardsams success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];