我试图在AFNetworking 3.0的POST请求中发送嵌套的可变字典作为参数。
这是我的sessionmanager设置:
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。我不想在服务器端进行任何更改。
答案 0 :(得分:0)
通过将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"];