0

我正在使用以下代码。

+(void)getQuarterList:(NSString *)user_id
{
    if ([self checkInternet])
    {
        NSString *url=[NSString stringWithFormat:@"%@/api/v1/quarters.json",MainURL];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *parameters = @{@"id":user_id};
      //  NSDictionary *parameters = @{};

        // NSDictionary *parameters = @{@"id":user_id,@"auth_token":auth_token};
        [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             NSDictionary *dict=[[NSDictionary alloc]initWithDictionary:responseObject];
              //NSMutableArray *dict=[[NSMutableArray alloc]initWithArray:responseObject];
             NSLog(@"dict%@",dict);
             if ([dict valueForKey:@"Success"])
             {
                 NSNotification *notif1 = [NSNotification notificationWithName:@"quarterDetailsNotifier" object:[dict valueForKey:@"Success"]];
                 [[NSNotificationCenter defaultCenter] postNotification:notif1];
             }
             else if ([dict valueForKey:@"noData"])
             {
                 NSNotification *notif1 = [NSNotification notificationWithName:@"noDateNotifier" object:[dict valueForKey:@"Error"]];
                 [[NSNotificationCenter defaultCenter] postNotification:notif1];
             }

         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             NSNotification *notif1 = [NSNotification notificationWithName:@"quarterDetailsFailNotifier" object:error];
             [[NSNotificationCenter defaultCenter] postNotification:notif1];
         }];
    }
    else
    {
        NSNotification *notif1 = [NSNotification notificationWithName:@"internetFailNotifier" object:nil];
        [[NSNotificationCenter defaultCenter] postNotification:notif1];
    }
}

我有以下错误

2014-05-20 15:39:33.610 TMLP[2770:a0b] The internet is working via WIFI.
2014-05-20 15:39:35.733 TMLP[2770:a0b] Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8e4a1a0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set., NSUnderlyingError=0x8e65ca0 "Request failed: not found (404)"}
2014-05-20 15:39:35.734 TMLP[2770:a0b] -[NSError length]: unrecognized selector sent to instance 0x8e4a180

2014-05-20 15:39:35.737 TMLP[2770:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSError length]: unrecognized selector sent to instance 0x8e4a180'

*首先抛出调用栈:如何解决这个错误

4

2 回答 2

0

第一件事。
您获得的 JSON 文本应始终以“[”或“{”开头,以便解析器识别它。您收到此错误的原因显然是因为这没有实现。我建议您通过在线提供的 JSON 验证器检查您的 JSON 文本。
我建议的第二件事是用于 JSON 序列化/反序列化是

NSJSON序列化

示例如下:

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error: &error];

options:NSJSONReadingAllowFragments可能会解决您现在得到的解析片段的问题。如果这没有帮助,另一种选择是从获得的碎片字符串中提取正确的 JSON 字符串。这意味着从字符串的开头和结尾消除多余的不需要的字符。

一个例子是这样的:

NSURL *url=[NSURL URLWithString:@"yourURL"];
    NSData *response = [NSData dataWithContentsOfURL:url];
    NSString *badjsonData = [NSString stringWithUTF8String:[response bytes]];
    NSString *goodJsonData = [badjsonData substringFromIndex:76];
    NSString *finaljsonData = [goodjsonData substringToIndex:[goodjsonData length]-9];
    NSData *goodData = [finaljsonData dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:goodData options:NSJSONReadingAllowFragments error: &error];
于 2014-05-20T13:25:18.367 回答
0

error作为 的对象发布NSNotification。在通知处理程序中,您将其用作NSString或其他具有方法的对象length

于 2014-05-20T10:34:21.137 回答