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





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值