使用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就拋出了
?
?
轉載于:https://www.cnblogs.com/yjg2014/p/3821679.html
總結
以上是生活随笔為你收集整理的使用AFNetworking 报错提示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android中的ContentValu
- 下一篇: 从零开始编写自己的C#框架(20)——框