文章目录
  1. 1. Code=3840 和 Code=-1011 问题

Code=3840 和 Code=-1011 问题

使用AFNetworking进行POST请求网络解析的时候,我们会遇到解析报错问题,报错如下:

1
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set., NSUnderlyingError=0x7fd7bad02080 {Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: not found (404)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7fd7bd1c9250>

我们查看响应头内容中,发现:"Content-Type" = "text/html;charset=utf-8";,那么我们要使用二进制的解析方式进行解析,同时也不要设置什么接受格式;

1
2
3
4
5
6
7
8
9
10
11
12
13
NSURLSessionConfiguration *configuratio = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manage = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuratio];

NSMutableURLRequest * m_request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:"URL" parameters:@{@"key0":@"value0", @"key1":@"value1"} error:nil];
manage.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURLSessionDataTask *dataTas = [manage dataTaskWithRequest:m_request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]);
}
}];
[dataTas resume];

如果设置[m_request setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];后,可能会报错:

1
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)"
文章目录
  1. 1. Code=3840 和 Code=-1011 问题