JSON 中的无效类型写入 (NSConcreteMutableData)

Invalid type in JSON write (NSConcreteMutableData)

我正在尝试使用 AFNetworing 发送 JSON 请求,但以下代码给我 JSON text did not start with array or object and option to allow fragments not set 错误:

NSString *post = [[NSString alloc] initWithFormat:
                  @"{\"request\":\"login\",\"userName\":\"%@\",\"password\":\"%@\"}", userName, password];

NSData *parameters = [post dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *parameterDictionary =
[NSJSONSerialization JSONObjectWithData:parameters options:NSJSONReadingAllowFragments error:nil];

DDLogDebug(@"Data: %@", [parameterDictionary description]);

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

AFHTTPRequestOperation *operation =
[manager POST:WEB_SERVICE_URL parameters:parameterDictionary
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          DDLogDebug(@"LoginView - Success Response: %@", responseObject);
      }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          DDLogError(@"LoginView - Error Response: %@", [error description]);
      }];

[operation start];

这是 parameterDictionary 对象的日志输出:

{
    password = q;
    request = login;
    userName = q;
}

我查看了类似的错误问题,并尝试将 parameters 对象放入数组中,但这次我收到错误“Invalid type in JSON write (NSConcreteMutableData)

NSMutableArray *array = [NSMutableArray new];
[array addObject:parameterDictionary];

DDLogDebug(@"Data: %@", [parameterDictionary description]);

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

AFHTTPRequestOperation *operation =
[manager POST:WEB_SERVICE_URL parameters:array
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          DDLogDebug(@"LoginView - Success Response: %@", responseObject);
      }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          DDLogError(@"LoginView - Error Response: %@", [error description]);
      }];

我做错了什么?

更新:

我试过以下但没成功:

NSMutableDictionary *dictionary = [NSMutableDictionary new];
[dictionary setObject:@"login" forKey:@"request"];
[dictionary setObject:@"q" forKey:@"userName"];
[dictionary setObject:@"q" forKey:@"password"];

DDLogDebug(@"Dictionary: %@", [dictionary description]);
DDLogDebug(@"Json: %@", [dictionary JSONRepresentation]);

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];

AFHTTPRequestOperation *operation =
[manager POST:WEB_SERVICE_URL parameters:dictionary
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          DDLogDebug(@"LoginView - Success Response: %@", responseObject);
      }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          DDLogError(@"LoginView - Error Response: %@", [error description]);
      }];

更新 2:

这是我的 AFHTTPRequestOperation 在错误块上的样子:

<AFHTTPRequestOperation: 0x7fd881fa1200, state: isFinished, cancelled: NO request: <NSMutableURLRequest: 0x7fd881f9fd60> { URL: http://www.olcayertas.com/services.php }, response: <NSHTTPURLResponse: 0x7fd881d913b0> { URL: http://www.olcayertas.com/services.php } { status code: 200, headers {
    Connection = close;
    "Content-Length" = 1050;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Wed, 21 Jan 2015 10:28:45 GMT";
    Server = Apache;
    "X-Powered-By" = PleskLin;
} }>

JSON 似乎无法在 JSONlint.com 上验证。我会首先从那里确保 JSON 是正确的。

这是我输入的: {\"request\":\"login\",\"userName\":\"%@\",\"password\":\"%@\"}

我用它来检查 JSON:

if ([NSJSONSerialization isValidJSONObject:requestJSONContents]) { }

我已经通过在浏览器中检查我的网络服务解决了这个问题。问题出在我的 services.php 文件中。创建日志文件时出错,此错误返回非 JSON 响应,导致请求失败。我的完整工作代码在这里:

NSMutableDictionary *dictionary = [NSMutableDictionary new];
[dictionary setObject:@"login" forKey:@"request"];
[dictionary setObject:@"q" forKey:@"userName"];
[dictionary setObject:@"q" forKey:@"password"];

DDLogDebug(@"Dictionary: %@", [dictionary description]);
DDLogDebug(@"Json: %@", [dictionary JSONRepresentation]);

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
//[manager.securityPolicy setAllowInvalidCertificates:true];

AFHTTPRequestOperation *operation =
[manager POST:WEB_SERVICE_URL parameters:dictionary
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          DDLogDebug(@"LoginView - Success Response: %@", responseObject);
      }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          DDLogError(@"LoginView - Error Response: %@", [error description]);
          DDLogError(@"Error: %@",operation);
      }];

[operation start]

这是我的完整 services.php 文件,将其作为使用 AFNetworiking 和 PHP 服务传递和获取 JSON 的完整示例:

PHP web service that accepts JSON input and return JSON response