AFNetworking 1.x 将 JSON 发布到 PHP
Posted
技术标签:
【中文标题】AFNetworking 1.x 将 JSON 发布到 PHP【英文标题】:AFNetworking 1.x post JSON to PHP 【发布时间】:2013-12-09 14:12:22 【问题描述】:我正在尝试将NSArray
发布到我的 php Web 服务,并在服务器端将它们转换为 PHP 数组以更新我的数据库。
下面是我发送的参数的NSLog
。
params = (
param1 = 3;
param2 = "string1";
param3 = "2013-12-09";
,
param1 = 3;
param2 = "string1";
param3 = "2013-12-09";
);
但是,在我的 PHP 端,当我执行 var_dump
时,我似乎得到了 NULL
。
Objective-C 代码
// mutable array to store all the dictionaries
NSMutableArray *mutableParameters = [[NSMutableArray alloc] initWithCapacity:5];
for(Person *p in personArray)
NSMutableDictionary *personDict = [NSMutableDictionary dictionary];
[personDict setValue:[NSNumber numberWithInt:p.id] forKey:@"param1"];
[personDict setValue:p.strString forKey:@"param2"];
[personDict setValue:p.strSelectedDate forKey:@"param3"];
[mutableParameters addObject:personDict];
NSDictionary *parametersDict = [NSDictionary dictionaryWithObjectsAndKeys: mutableParameters, @"params", nil];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:WEBSERVICES_BASE_URL]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient postPath:@"clientEdit.php" parameters:parametersDict success:^(AFHTTPRequestOperation *operation, id responseObject)
// success
NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Request Successful, response '%@'", responseStr);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
// failure
NSLog(@"Error: %@", error.localizedDescription);
];
PHP 代码
$params = $_REQUEST['params'];
var_dump($params);
【问题讨论】:
var_dump($_REQUEST);显示什么? @user574632 它显示array(0)
好的,所以这是客户端问题,而不是服务器(php)。不是 obj-c 编码器,但我无法在您的代码中看到您实际提出请求的位置。
@user574632 我已将部分代码添加到 Objective C 部分。即使您不是 obj-c 编码器,也感谢您愿意提供帮助。真的很感激。
【参考方案1】:
使用this SO thread.中高亮的包裹到NSDictionary
的方法后解决了另外,我将参数编码设置为AFFormURLParameterEncoding
而不是AFJSONParameterEncoding
。不确定这是否甚至接近正确的执行方式,但它仍然有效。
【讨论】:
【参考方案2】:首先在您的 OBJ-C 部分使用您的参数创建一个 NSMutableArray(我这样做):
-(NSMutableArray *)getNumbers
NSMutableArray * tempArray = [[NSMutableArray alloc]init];
for (int i = 0; i < 10; i++)
NSDictionary * dict = [[NSDictionary alloc]initWithObjectsAndKeys:@"key",[NSString stringWithFormat:@"%i",i]];
[tempArray addObject:dict];
return tempArray;
NSDictionary * paramDict = @
@"action" : @"updatePedido",
@"array" : [self getNumbers],
;
然后这样称呼它
NSArray *info = @[paramDict];
NSError * error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:&error];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"teste.php"]];
// Create request body
NSMutableData *body = [NSMutableData data];
// Append of the body with your data
[body appendData:jsonData];
[request setHTTPBody:body];
// Set request method POST
[request setHTTPMethod:@"POST"];
// Set request Headers
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:request.URL];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
NSError *error;
NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"response String %@",responseString);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@",error.localizedDescription);
];
[operation start];
在服务器 PHP 端:
<?php
header("Content-type: application/json");
$data = file_get_contents("php://input");
$result = json_decode($data);
foreach ($result as $obj)
$array = $obj->array;
for($j = 0; $j < count($array); $j++)
$number = $array[$j]->key;
echo $number;
?>
我只是说我使用的是 AFNetworking 2.0,我建议你考虑升级它。
【讨论】:
以上是关于AFNetworking 1.x 将 JSON 发布到 PHP的主要内容,如果未能解决你的问题,请参考以下文章
JSON 文本没有以数组开头 AFNetworking 错误
IOS AFNetworking的使用与YYModel解析JSON数据
使用 AFNetworking 将 JSON 解析为 NSDictionary