【问题标题】:Invalid type in JSON write (NSConcreteMutableData)JSON写入中的类型无效(NSConcreteMutableData)
【发布时间】:2015-03-19 17:26:44
【问题描述】:

我正在尝试使用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 http-post afnetworking-2


    【解决方案1】:

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

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

    我用它来检查 JSON:

    if ([NSJSONSerialization isValidJSONObject:requestJSONContents]) { }

    【讨论】:

    • @givan 这是我作为参数传递的构建的 NSString,它是有效的:'{ "request" : "login" , "userName" : "q" , "password" : "q " }'
    • @OlcayErtaş 在字符串中不带斜杠的情况下运行代码仍会产生相同的错误?我已经用您可以执行的检查更新了我的答案。不确定您是否已经看到:stackoverflow.com/questions/21452385/…
    【解决方案2】:

    我通过在浏览器中检查我的网络服务解决了这个问题。问题出在我的 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-16
      • 2019-10-26
      • 2021-08-08
      • 2016-03-16
      • 2021-12-26
      • 2017-07-05
      • 2017-03-05
      • 2017-01-24
      相关资源
      最近更新 更多