zoukankan      html  css  js  c++  java
  • 使用AFNetworking 报错提示

    使用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就抛出了

  • 相关阅读:
    17 Letter Combinations of a Phone Number(medium)
    16 3Sum closest(medium)
    15 3Sum(medium)
    linux环境下搭建自动化Jenkins管理工具
    Danjgo学习笔记(五)----Django数据库的查询
    Danjgo学习笔记(五)----Django中表的关系
    Django常见的Field
    selenium+python+ip池 实现博客园刷博客浏览量
    Danjgo学习笔记(五)----常见模板过滤器和自制过滤器
    Danjgo学习笔记(四)---danjgo框架内的常用标签
  • 原文地址:https://www.cnblogs.com/yjg2014/p/3821679.html
Copyright © 2011-2022 走看看