在 AFNetworking 3.0 中发送嵌套的 NSMutableDictionary 作为 POST 请求的参数
Sending Nested NSMutableDictionary as parameters for POST request in AFNetworking 3.0
我正在尝试将嵌套可变字典作为参数发送到 AFNetworking 3.0 中的 POST 请求中。
这是我的会话管理器设置:
self.requestSerializer = [AFJSONRequestSerializer serializer];
self.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
词典准备如下:
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterNoStyle;
NSNumber *myNumber = [f numberFromString:self.userMobNoTf.text];
NSString *UDID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSMutableDictionary *miniDict = [NSMutableDictionary dictionary];
[miniDict setObject:[NSNumber numberWithInteger:0] forKey:@"ID"];
[miniDict setObject:self.userIDTf.text forKey:@"UserId"];
[miniDict setObject:self.userNameTf.text forKey:@"Name"];
[miniDict setObject:self.userPasswordTf.text forKey:@"Password"];
[miniDict setObject:UDID forKey:@"IMEI"];
[miniDict setObject:@"1.0" forKey:@"AppVersion"];
[miniDict setObject:self.userNameTf.text forKey:@"CreatedBy"];
[miniDict setObject:myNumber forKey:@"MobileNo"];
[miniDict setObject:[NSNumber numberWithInt:1] forKey:@"isActive"];
NSMutableDictionary *paramDict = [NSMutableDictionary dictionary];
[paramDict setObject:self.userNameTf.text forKey:@"UserName"];
[paramDict setObject:self.userPasswordTf.text forKey:@"Password"];
[paramDict setObject:self.userIDTf.text forKey:@"UserId"];
[paramDict setObject:myNumber forKey:@"MobileNo"];
[paramDict setObject:UDID forKey:@"Imei"];
[paramDict setObject:[miniDict mutableCopy] forKey:@"Food_UserLogin"];
这是 POST 请求:
[appD.sessionManager POST:@"FoodSignUpSave" parameters:paramDict progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
dispatch_async(dispatch_get_main_queue(), ^{
[radialHUD dismiss];
});
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
dispatch_async(dispatch_get_main_queue(), ^{
[radialHUD dismiss];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:error.description delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
});
}];
但是,我遇到以下错误:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x174660fc0 {NSDebugDescription=Invalid value around character 0., NSUnderlyingError=0x174241bc0 "Request failed: bad request (400)"}
如果我不在密钥 "Food_UserLogin" 中发送 miniDict,我会收到成功响应,但将此参数作为字典放置会产生问题。关于哪里出了问题有什么建议吗?
P.S。我不想在服务器端进行任何更改。
通过将 miniDict 转换为 JSONString 并将此 JSONString 作为值发送到 "Food_UserLogin" 中解决了问题。
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:miniDict options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[paramDict setObject:jsonString forKey:@"Food_UserLogin"];
我正在尝试将嵌套可变字典作为参数发送到 AFNetworking 3.0 中的 POST 请求中。
这是我的会话管理器设置:
self.requestSerializer = [AFJSONRequestSerializer serializer];
self.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
词典准备如下:
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterNoStyle;
NSNumber *myNumber = [f numberFromString:self.userMobNoTf.text];
NSString *UDID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSMutableDictionary *miniDict = [NSMutableDictionary dictionary];
[miniDict setObject:[NSNumber numberWithInteger:0] forKey:@"ID"];
[miniDict setObject:self.userIDTf.text forKey:@"UserId"];
[miniDict setObject:self.userNameTf.text forKey:@"Name"];
[miniDict setObject:self.userPasswordTf.text forKey:@"Password"];
[miniDict setObject:UDID forKey:@"IMEI"];
[miniDict setObject:@"1.0" forKey:@"AppVersion"];
[miniDict setObject:self.userNameTf.text forKey:@"CreatedBy"];
[miniDict setObject:myNumber forKey:@"MobileNo"];
[miniDict setObject:[NSNumber numberWithInt:1] forKey:@"isActive"];
NSMutableDictionary *paramDict = [NSMutableDictionary dictionary];
[paramDict setObject:self.userNameTf.text forKey:@"UserName"];
[paramDict setObject:self.userPasswordTf.text forKey:@"Password"];
[paramDict setObject:self.userIDTf.text forKey:@"UserId"];
[paramDict setObject:myNumber forKey:@"MobileNo"];
[paramDict setObject:UDID forKey:@"Imei"];
[paramDict setObject:[miniDict mutableCopy] forKey:@"Food_UserLogin"];
这是 POST 请求:
[appD.sessionManager POST:@"FoodSignUpSave" parameters:paramDict progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
dispatch_async(dispatch_get_main_queue(), ^{
[radialHUD dismiss];
});
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
dispatch_async(dispatch_get_main_queue(), ^{
[radialHUD dismiss];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:error.description delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
});
}];
但是,我遇到以下错误:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x174660fc0 {NSDebugDescription=Invalid value around character 0., NSUnderlyingError=0x174241bc0 "Request failed: bad request (400)"}
如果我不在密钥 "Food_UserLogin" 中发送 miniDict,我会收到成功响应,但将此参数作为字典放置会产生问题。关于哪里出了问题有什么建议吗?
P.S。我不想在服务器端进行任何更改。
通过将 miniDict 转换为 JSONString 并将此 JSONString 作为值发送到 "Food_UserLogin" 中解决了问题。
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:miniDict options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[paramDict setObject:jsonString forKey:@"Food_UserLogin"];