【问题标题】:Getting JSON text did not start with array or object and option to allow fragments not set获取 JSON 文本未以数组或对象开头,并且允许未设置片段的选项
【发布时间】:2017-03-21 01:50:05
【问题描述】:

有什么方法可以在我的以下登录代码中打印/NSLOG API 返回的字符串或数组:

-(void)loginToAPI:(NSString *)email password:(NSString *)password {

    NSString *controller = @"login";
    NSString *action =  @"authenticate";

    NSDictionary *params = @{@"username": email,
                             @"userpass": password,
                             @"controller":  controller,
                             @"action": action,
                             @"app_id": APP_ID,
                             @"app_key": APP_KEY};
    NSLog(@"params %@", params);

    if ([self isNetworkAvailable]) {
        AFHTTPRequestOperationManager *client = [AFHTTPRequestOperationManager manager];
        client.responseSerializer.acceptableContentTypes = [client.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
        [client POST:[[Config sharedInstance] getAPIURL]
          parameters:params
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 NSDictionary *jsonObject= responseObject;
                 NSString *status = [jsonObject objectForKey:@"status"];
                 NSLog(@"Request Successful, response '%@'", jsonObject);
                 if ([status isEqualToString:@"success"]) {
                     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                     NSDictionary *data = [jsonObject objectForKey:@"data"];
                     NSDictionary *userDict = [data objectForKey:@"user"];
                     NSDictionary *djsaDict = [data objectForKey:@"djsa"];

                     User *currentUser = [[User alloc] initWithProperties:userDict];

                     [currentUser save];

                     DJSA_Cutomer *djsa = [[DJSA_Cutomer alloc] initWithProperties:djsaDict];


                     NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:currentUser];
                     NSData *djsaData = [NSKeyedArchiver archivedDataWithRootObject:djsa];

                     [userDefaults setObject:userData forKey:@"currentUser"];
                     [userDefaults setObject:djsaData forKey:@"djsa_customer"];
                     [userDefaults synchronize];

                     [[NSNotificationCenter defaultCenter] postNotificationName: @"LOGIN_SUCCESS" object:currentUser userInfo:nil];

                 } else {
                     [[NSNotificationCenter defaultCenter] postNotificationName: @"LOGIN_FAILED" object:nil userInfo:nil];
                 }
             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 NSLog(@"Request Failed with Error - loginToAPI: %@, %@", error, error.userInfo);
                 [[NSNotificationCenter defaultCenter] postNotificationName: @"SERVER_ERROR" object:nil userInfo:nil];
             }];
    }
}

它总是失败并进入“请求失败并出现错误 - loginToAPI”部分。是否可以查看服务器返回的实际值以便诊断问题?

谢谢!

【问题讨论】:

  • "Request Failed with Error - loginToAPI"的完整日志是什么?如果需要,您可以隐藏您的网址。如果我没记错的话,AFNetworking 将错误(和数据)封装在其中。

标签: ios objective-c json afnetworking


【解决方案1】:

我遇到了同样的问题。在我的情况下,解决方案是

client.responseSerializer = [AFHTTPResponseSerializer serializer];

我这样做了,但我仍然遇到同样的错误。问题是我设置的是 client.requestSerializer 而不是 client.responseSerializer。小错误,但需要很多时间。

【讨论】:

    【解决方案2】:

    您可以从操作对象的响应属性中获取状态代码,这应该可以让您了解发生了什么问题:

    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Request Failed with Error - loginToAPI: %@, %@", error, error.userInfo);
        NSLog(@"%@", [NSHTTPURLResponse localized​String​For​Status​Code:operation.response.statusCode]);
        [[NSNotificationCenter defaultCenter] postNotificationName: @"SERVER_ERROR" object:nil userInfo:nil];
    }];
    

    否则,最好使用Charles web proxy 之类的工具来检查实际的请求和响应。它有一个免费试用版,应该足以满足您的需求,而且设置相对容易。

    【讨论】:

      【解决方案3】:

      替换这一行:

      client.responseSerializer.acceptableContentTypes = [client.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
      

      用这个:

      [client.securityPolicy setValidatesDomainName:NO];
      [client.securityPolicy setAllowInvalidCertificates:YES];
      client.responseSerializer = [AFHTTPResponseSerializer serializer];
      

      【讨论】:

        猜你喜欢
        • 2016-03-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-07
        • 1970-01-01
        • 2018-05-21
        • 2014-01-06
        • 1970-01-01
        相关资源
        最近更新 更多