AFNetworking Error Domain=NSCocoaErrorDomain Code=3840 “Unescaped control character around character 3295.” 错误
在做某第三方客户端的时候, 使用 AFNetworking, 发送请求拿到的结果跑进
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
输出错误:
|
|
Note: 在
failure block
里面依旧可以拿到返回数据, 然后进行处理.但这并非良好的解决方案
原因
错误信息中写得较明白, 是返回数据存在 escaped
这是 JSON
解析时候的报错, AFNetworking
使用 NSJSONSerialization
对 JSON
字符串进行解析, 而控制符 escaped
会导致其报错.
解决方案
-
找到
AFNetworking
包中的AFURLResponseSerialization.m
文件定位到该方法:
- (id)responseObjectForResponse:(NSURLResponse *)response data:(NSData *)data error:(NSError *__autoreleasing *)error
找到下面代码段
123@autoreleasepool {NSString *responseString = [[NSString alloc] initWithData :data encoding :stringEncoding];if (responseString && ![responseString isEqualToString:@ " "]) { -
可以看到注释中有阐述该问题
12// 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:@" "]) {
之后添加:
12345678910NSCharacterSet *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/