Php 如何在NSDictionary中发布NSArray的NSDictionary而没有问题?
我知道怎么做,很简单 问题是它不起作用 以下是我用来发布数据的函数:Php 如何在NSDictionary中发布NSArray的NSDictionary而没有问题?,php,ios,objective-c,json,afnetworking-2,Php,Ios,Objective C,Json,Afnetworking 2,我知道怎么做,很简单 问题是它不起作用 以下是我用来发布数据的函数: - (void)updateWebsitesUsingParameters:(NSDictionary *)parameters; { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:@"http://notreal/updateWebsites.php"
- (void)updateWebsitesUsingParameters:(NSDictionary *)parameters;
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://notreal/updateWebsites.php"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
//...
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//...
}];
}
以下是参数:
NSDictionary *parameters = @{@"type" : @"0",
@"credentials" : @{@"email" : @"notreal@gmail.com", @"password" : @"notreal"},
@"device" : @{@"ID" : @"8588107756600540", @"numberOfSessions" : @"0", @"name" : @"Nick's iMac"},
@"websites" : @[@{@"title" : @"Google", @"URL" : @"http://www.google.com"}, @{@"title" : @"Yahoo", @"URL" : @"http://www.yahoo.com"}]};
NSDictionary *parameters = @{@"websites" : @[@{@"Title" : @"Google", @"URL" : @"http://www.google.com"}, @{@"Title" : @"Yahoo", @"URL" : @"http://www.yahoo.com"}]};
以下是MySQL字段中保存的内容:
[{URL = "http://www.google.com";},{title = Google;},{URL = "http://www.yahoo.com";},{title = Yahoo;}]
这太疯狂了强>
@[@{@"title" : @"Google"}, @{@"title" : @"Yahoo"}]
这并不是:
@[@{@"title" : @"Google", @"URL" : @"http://www.google.com"}, @{@"title" : @"Yahoo", @"URL" : @"http://www.yahoo.com"}]
以下是回应:
{
websites = (
{
URL = "http://www.google.com";
},
{
title = Google;
},
{
URL = "http://www.yahoo.com";
},
{
title = Yahoo;
}
);
}
疯了强>
由于某种原因,如果我添加一个额外的属性,它就会崩溃
这一定是一个错误,因为它毫无意义
编辑:
我可以:
{
websites = (
{
URL = "http://www.google.com";
},
{
title = Google;
},
{
URL = "http://www.yahoo.com";
},
{
title = Yahoo;
}
);
}
这是从应用程序发送的实际服务器响应,中间没有MySQL。
编辑3:REQUEST: <NSMutableURLRequest: 0x7f9352d467e0> { URL: http://notreal/updateWebsites.php }
请求:{URL:http://notreal/updateWebsites.php }
HTTPBody如下所示:
<63726564 656e7469 616c735b ... 653d30>
我怎样才能破译这个
另外,我使用的是一个AFHTTPRequestSerializer。也许,如果我将其更改为AFJSONRequestSerializer,它会解决问题,但我真的不想这样做,因为我已经用这种方式编写了许多方法。“查询字符串参数化根本不是编码嵌套数据结构的可靠方法。这就是为什么所有现代web框架都内置了将传入的JSON自动解码为参数的便利。”——Mattt Thompson
所以,JSON它是
参数:
NSDictionary *parameters = @{@"type" : @"0",
@"credentials" : @{@"email" : @"notreal@gmail.com", @"password" : @"notreal"},
@"device" : @{@"ID" : @"8588107756600540", @"numberOfSessions" : @"0", @"name" : @"Nick's iMac"},
@"websites" : @[@{@"title" : @"Google", @"URL" : @"http://www.google.com"}, @{@"title" : @"Yahoo", @"URL" : @"http://www.yahoo.com"}]};
NSDictionary *parameters = @{@"websites" : @[@{@"Title" : @"Google", @"URL" : @"http://www.google.com"}, @{@"Title" : @"Yahoo", @"URL" : @"http://www.yahoo.com"}]};
发送:
检索:
<?php
header('Content-type: application/json');
$request = json_decode(file_get_contents('php://input'), TRUE);
$response = ["URLOfTheSecondWebsite" => $request['websites'][1]['URL']];
echo json_encode($response);
?>
全部完成
纯金。更新函数
AFQueryStringPairsFromKeyAndValue
改变
for (id nestedValue in array) {
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)];
}
到
与AFHTTPSessionManager有相同的问题。 不幸的是,您必须切换到
AFJSONRequestSerializer
才能发送字典数组:
manager.requestSerializer = [AFJSONRequestSerializer serializer];
AFJSONRequestSerializer
设置application/json内容类型和期望json的服务器将成功接受请求参数,而AFHTTPRequestSerializer
将始终在标题中包含text/html内容类型,即使您明确设置了它。尝试发送您发送的参数作为响应发送到服务器。这样,您可以查看问题是否在iOS或PHP上。您是否尝试设置requestSerializer?不,我不需要为超过10个方法设置requestSerializer。现在,也许我需要它,但如何解决?什么设置可以解决此问题?当您说它不工作时,您是什么意思?您是否尝试在t上记录请求服务器端查看您得到了什么或什么都没有得到?您可以在iOS端打印请求正文并将其添加到问题中吗?请求应该在操作中(operation.request
)这个答案应该钉在主页上。它可以让像我这样的新手轻松地摆脱所有挫折……:“)正义将得到伸张。真相将比我的同伴们所超越的闪闪发光的意识世界更加耀眼。
manager.requestSerializer = [AFJSONRequestSerializer serializer];