AFNetworking Post域名:@“NSCocoaErrorDomain” - 代码:3840

时间:2015-11-27 06:06:58

标签: ios objective-c iphone json

我正在尝试在WS上实现一个POST,它使用JArray.Parse(string)管理其序列化,我必须为其发送序列化字符串。但在发送时它返回我的错误:

  

错误NSError * domain:@“NSCocoaErrorDomain” - 代码:3840   0x00007fd520e469f0解析时遇到意外的字符   值:=。路径'',第0行,第0位。'

     

缓存控制:私有传输编码:分块允许:POST   内容类型:text / html; charset = UTF-8服务器:Microsoft-IIS / 8.5   X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET日期:星期五,11月27日   2015 04:44:18 GMT

我留下我的代码:

 NSMutableDictionary *tmp = [[NSMutableDictionary alloc] initWithObjectsAndKeys: .......nil];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"JSON Output: %@", jsonString);
    AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
    [policy setAllowInvalidCertificates:YES];
    AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
    [operationManager setSecurityPolicy:policy];
    [operationManager POST:@"URLPOST" parameters:jsonData success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", [responseObject description]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", operation.responseString);
        NSLog(@"Error: %@", operation.responseData);
        NSLog(@"Error: %@", operation.responseObject);
        NSLog(@"Error: %@", operation.responseSerializer);
        NSLog(@"JSON Error: %@", [error localizedDescription]);

    }];

正如您所看到的,我正在将NSMutableDictionary转换为NSData,然后转换为NSString我将返回:{},{}。 E使用manager.requestSerializer = [AFJSONRequestSerializer serializer];测试并且未成功。此外,我想知道同样的序列化字符串是如何如此; [{},{}]

编辑

尝试失败几个小时之后,AFNetworking开始尝试除已存在的代码之外的其他东西可以修复错误,留下代码的一部分以防其他人遇到同样的问题:

NSMutableDictionary *tmp = [[NSMutableDictionary alloc] initWithObjectsAndKeys: ...nil];
NSArray *tmpArray = [NSArray arrayWithObject:tmp];
NSData *JsonData = [NSJSONSerialization dataWithJSONObject:tmpArray options:0 error:NULL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[JsonData length]];
    [request setURL:[NSURL URLWithString:@"URL"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"Close" forHTTPHeaderField:@"Connection"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"text/plain" forHTTPHeaderField:@"Content-type"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:JsonData];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"response - %@",responseString);

0 个答案:

没有答案