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,我会得到成功响应,但是将此参数作为字典会产生问题。关于什么是错的任何建议?

PS我不想在服务器端进行任何更改。

4

1 回答 1

1

通过将 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"];
于 2016-03-30T09:50:01.873 回答