今天在与后台调接口的时候,遇到一个问题,使用AFNetworking报错,具体如下:
请求失败: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=0x60800024cd20 {Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unsupported media type (415)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x608000235d00> { URL: https://192.168.1.42/siweb/iface/user/checkLogin } { status code: 415, headers {**
Connection = "keep-alive";**
"Content-Language" = en;**
"Content-Length" = 1048;**
"Content-Type" = "text/html;charset=utf-8";**
Date = "Mon, 12 Dec 2016 06:45:00 GMT";**
Server = "nginx/1.10.2";**
} }, NSErrorFailingURLKey=https://192.168.1.42/siweb/iface/user/checkLogin, com.alamofire.serialization.response.error.data**
为什么会出现这个报错呢?百度了很多资料,然而并没有找到好的解决方法。
以下为尝试的解决方法:
1>修改AFNetworking内部,这个文件AFURLResponseSerialization.m中修改代码就能解决:
第223行,初始化时,将如下代码:
1 self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
修改成
1 self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil nil];
结果,并没有什么用,我不否认,可能可以解决某些人遇到的问题,说是AFNetworking不支持以"text/html"格式的"content-type",反正我是添加了,没啥用;
2>在创建请求管理者(manger)是,做一些设置。在封装的时候添加了这两行代码
结果还是不行
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer = [AFHTTPRequestSerializer serializer];
// post请求
+ (void)POST:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer = [AFHTTPRequestSerializer serializer];
[manager POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
//
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if(success)
{
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if(error)
{
failure(error);
}
}];
}
3>为了解决这个问题尝试了很多方法。后来在对请求数据格式和响应数据格式初始化的时候,将之前的父类(AFHTTPRequestSerializer)换成了它的子类(AFJSONRequestSerializer)
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
这次请求成功了,数据也出来。
总结:成功以后,我把添加的代码去掉,请求也成功,其实并不是我们设置请求报文时,没设置接受“text/html”的问题。因为后台返回的响应报文的接受类型就是@“application/json”类型的,所以AFN已经设置好了,与这个无关。
1 self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil nil];
但为什么会出现这个问题呢?我猜测应该是后台没有设置好返回的类型是什么,导致出现这样的状况,所以我们得一开始就设置初始化请求的格式和返回的格式
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];