gpt4 book ai didi

php - 使用 AfNetworking 2 将数据发布到 PHP 时,带有 NSArray 的 NSDictionary 正在转换为元素数组

转载 作者:行者123 更新时间:2023-11-29 10:48:07 25 4
gpt4 key购买 nike

NSDictionary *partyOne = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:2], @"type",
[NSNumber numberWithInt:1], @"status",
@"name", @"name",
@"name", @"nid",
@"name", @"nationality",
@"", @"address",
@"name", @"mobile",
nil];

NSDictionary *partyTwo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:2], @"type",
[NSNumber numberWithInt:1], @"status",
@"name", @"name",
@"name", @"nid",
@"name", @"nationality",
@"", @"address",
@"name", @"mobile",
nil];


NSArray *partyArray = [NSArray arrayWithObjects: partyOne, partyTwo, nil];

NSDictionary *finalData =[NSDictionary dictionaryWithObject: partyArray forKey:@"details"];

当我将上面的 NSDictionary 发送到 AFNetworking 2 时,如下所示,

// Send the data to server
NSDictionary *regDic = [self generteJasonData];

// Send the registered data to server
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:userName password:userpassWd];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager setShouldUseCredentialStorage:NO];

NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"POST" URLString:@"http://testlink/submit"parameters:regDic];
[request setHTTPShouldHandleCookies:NO];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
AFJSONResponseSerializer *serializer = [AFJSONResponseSerializer serializer];
[operation setResponseSerializer:serializer ];
[operation setShouldUseCredentialStorage:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
[manager.requestSerializer clearAuthorizationHeader];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure: %@ responce :%@", error,operation.responseString);
[manager.requestSerializer clearAuthorizationHeader];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"upload", @"") message:NSLocalizedString(@"Some error occureed while uploading data, please tryagain later", @"")
delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];


}];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[operation start];

我在我的 php 服务器上收到它,

details =             (
{
address = "";
},
{
mobile = name;
},
{
name = name;
},
{
nationality = name;
},
{
nid = name;
},
{
status = 1;
},
{
type = 2;
},
{
address = "";
}, {
mobile = name;
}, {
name = name;
},
{
nationality = name;
},
{
nid = name;
},
{
status = 1;
},
{
type = 2;
},
);

即我已经发送了 2 个数组元素,但我收到了同样多的数组元素。任何人都可以让我知道这里的问题是什么。他们是 AFNetworking 2.0 API 的问题还是我的代码有问题。

最佳答案

好的,所以您需要做的是修复方法

NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value)

在文件 AFURLRequestSerialization.m 中:而不是

for (id nestedValue in array)

成功了

for (int i = 0;i < [array count];++i)

然后代替

[mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)];

成功了

[mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[%d]", key,i], nestedValue)];

关于php - 使用 AfNetworking 2 将数据发布到 PHP 时,带有 NSArray 的 NSDictionary 正在转换为元素数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21657149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com