AFNetworking3.x Error Code:3840
2017-06-08 本文已影响167人
小北风sky
Error: 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.}
解决办法:
- 设置请求管理者,因为传递过去和接收回来的数据都不是json类型的,所以在这里要设置为AFHTTPRequestSerializer和AFHTTPResponseSerializer
manager.requestSerializer = [AFHTTPRequestSerializer serializer];// 请求
manager.responseSerializer = [AFHTTPResponseSerializer serializer];// 响应
- 在success中将responseObject转换为一个字符串
//转码
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
- 整体代码如下:
///AFNetworking 3.x
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];// 请求
manager.responseSerializer = [AFHTTPResponseSerializer serializer];// 响应
[manager POST:urlString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
//转码
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"JSON: %@", string);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];