AFNetworking Error Domain=NSCocoaErrorDomain Code=3840

AFNetworking Error Domain=NSCocoaErrorDomain Code=3840

AFNetworking Error Domain=NSCocoaErrorDomain Code=3840 “Unescaped control character around character 3295.” 错误

在做某第三方客户端的时候, 使用 AFNetworking, 发送请求拿到的结果跑进

failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure

输出错误:

         
         
1
2
3
         
         
Error Domain=NSCocoaErrorDomain Code= 3840
"Unescaped control character around character 3295."
UserInfo={NSDebugDescription=Unescaped control character around character 3295.};

Note: 在 failure block 里面依旧可以拿到返回数据, 然后进行处理.

但这并非良好的解决方案

原因

错误信息中写得较明白, 是返回数据存在 escaped

这是 JSON 解析时候的报错, AFNetworking 使用 NSJSONSerialization 对 JSON 字符串进行解析, 而控制符 escaped 会导致其报错.

解决方案

  • 找到 AFNetworking 包中的 AFURLResponseSerialization.m 文件

    定位到该方法:

    - (id)responseObjectForResponse:(NSURLResponse *)response
                           data:(NSData *)data
                          error:(NSError *__autoreleasing *)error
    

    找到下面代码段

               
               
    1
    2
    3
               
               
    @autoreleasepool {
    NSString *responseString = [[NSString alloc] initWithData :data encoding :stringEncoding];
    if (responseString && ![responseString isEqualToString:@ " "]) {
  • 可以看到注释中有阐述该问题

               
               
    1
    2
               
               
    // Workaround for a bug in NSJSONSerialization when Unicode character escape codes are used instead of the actual character
    // See http://stackoverflow.com/a/12843465/157142

    具体可是参照给出的连接 http://stackoverflow.com/a/12843465/157142

    在这里我们不采用里面的方案, 而使用去除控制符的方式

  • 在 if (responseString && ![responseString isEqualToString:@" "]) {

    之后添加:

               
               
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
               
               
    NSCharacterSet *controlChars = [ NSCharacterSet controlCharacterSet];
    NSRange range = [responseString rangeOfCharacterFromSet:controlChars];
    if (range.location != NSNotFound) {
    NSMutableString * mutable = [ NSMutableString stringWithString:responseString];
    while (range.location != NSNotFound) {
    [ mutable deleteCharactersInRange:range];
    range = [ mutable rangeOfCharacterFromSet:controlChars];
    }
    responseString = mutable;
    }

    该段代码获取所有控制符, 并从返回结果的字符串中去除它们.

    //其他报错

    使用AFNetworking 框架 解析数据 报错提示

    数据请求失败

    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=0x9b7eba0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

    解决:

    AFNetWorking的JSON解析默认库是使用的AFJSONRequestOperation模式,只支持text/json,application/json,text/javascript,所以如果出现code=-1016错误则说明当前的JSON解析模式是text/html,所以要加上这段代码:

    [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];//加上这句话

     

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",kDMBaseURL]];

        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];//加上这句话

        AFJSONRequestOperation *jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

            DMLog(@"str %@",JSON); 

        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

            DMLog(@"数据请求失败%@",error);

        }];

        [jsonOperation start];

    让他支持text/html模式就可以解决了

     

    数据请求失败

    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=0x9ba1b30 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

    解决:

    后台返回的不是一个有效的JSON string 所以库里面转化的时候一个error就抛出了

    就是最后一句说的,肯定是服务器端,在你指定的输出 JSON 数据之前截断输出其他数据,所以出现错误。可以去服务器段判断。

  • 再次编译执行, 问题就解决了.转自:http://tianyiyan.com/2015/11/29/AFNetworking-Error-Domain-NSCocoaErrorDomain-Code-3840/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值