使用 afnetworking 2.0 发送字典数组
Posted
技术标签:
【中文标题】使用 afnetworking 2.0 发送字典数组【英文标题】:Sending array of dictionary with afnetworking 2.0 【发布时间】:2020-11-02 22:55:18 【问题描述】:我有一个大问题,我正在尝试使用 afnetworking 发送一个字典数组,这是我的代码:
checkDeliveryParams = @@"cart_total":@(cart_total),@"shipping":@@"address":[User sharedInstance].shipping_city,@"country":@"IT",@"postcode":[User sharedInstance].shipping_postcode,@"state":[User sharedInstance].shipping_provincia,@"user_id":@([User sharedInstance].user_id),@"cart_contents":temp;
购物车总数是这样的数组
(
quantity:1
product_id:2
)
但是服务器收到类似这样的东西
(
quantity:1
),
(
product_id:2
)
我的请求代码是
+(void)GET:(NSString *)path
params:(NSDictionary *)params
auth:(BOOL)authenticate
success:(void (^)(NSDictionary* responseObject))success
failure:(void (^)(NSError *error, NSDictionary* responseObject))failure
NSString *URLString = [@"" stringByAppendingPathComponent:path];
NSLog(@"GET - API URL: %@", URLString);
AFHTTPRequestOperationManager *manager = [Rest commonManager:path sendAuthToken:authenticate];
// manager.requestSerializer=NSJSONSerialization ;
[manager GET:URLString parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary *responsedata)
NSLog(@"ok della richiesta %@",responsedata);
[Rest verbose:responsedata];
success(responsedata);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"errore della richiesta %@",error.localizedDescription);
failure(error,[Rest parseErrorResponseForOperation:operation]);
];
试过很多方法,试过设置nsjonserializer,"application/json",试过用NSURLSessionDataTask...
对不起我的英语,有人可以帮助我吗?
[rest commonManager] 中的公共管理器调用在此方法中定义
+(AFHTTPRequestOperationManager *)commonManager:(NSString *)path sendAuthToken:(BOOL)sendToken
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.securityPolicy.allowInvalidCertificates = NO;
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
if([Session isOpen] && [Rest authorizationRequiredForPath:path] && sendToken)
[manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@", [Session token]] forHTTPHeaderField:@"Authorization"];
return manager;
【问题讨论】:
cart_total
很奇怪。你能澄清一下它的价值吗?因为quantity:1
,那是什么?一个字符串? “键/值”字典?你的NSLog(@"Cart Total: %@", cart_total)
可以吗?
对不起,我错了,数组是购物车内容而不是购物车总数
不清楚。你能做NSData *jsonData = [NSJSONSerialization jsonDataWithObject: checkDeliveryParams options: 0 error: nil]; NSLog(@"JSON Sending: %@", [[NSString alloc] initWithData: jsonData])
并给出输出吗?
JSON 发送:"cart_contents":["product_id":10155,"quantity":1,"product_id":17960,"quantity":1],"user_id": 91,"shipping":"country":"IT","state":"PA","city":"palermo","postcode":"90124","address":"via giovanni gentile 3" ,"cart_total":119.30000305175781
但是服务器没有收到这种格式的,为什么?
【参考方案1】:
这就是我在 get 调用中发送参数的方式
+(void)GET:(NSString *)path
params:(NSDictionary *)params
auth:(BOOL)authenticate
success:(void (^)(NSDictionary* responseObject))success
failure:(void (^)(NSError *error, NSDictionary* responseObject))failure
NSString *URLString = [@"" stringByAppendingPathComponent:path];
NSLog(@"GET - API URL: %@", URLString);
AFHTTPRequestOperationManager *manager = [Rest commonManager:path sendAuthToken:authenticate];
// manager.requestSerializer=NSJSONSerialization ;
[manager GET:URLString parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary *responsedata)
NSLog(@"ok della richiesta %@",responsedata);
[Rest verbose:responsedata];
success(responsedata);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"errore della richiesta %@",error.localizedDescription);
failure(error,[Rest parseErrorResponseForOperation:operation]);
];
【讨论】:
以上是关于使用 afnetworking 2.0 发送字典数组的主要内容,如果未能解决你的问题,请参考以下文章
AFNetworking 2.0 发送带有字典参数数组的发布请求
AFNetworking 2.0 参数字典不适用于 GET 请求
使用 AFNetworking 2.0 发送 POST 数据