上一篇文章中封装的方法体是对于常用服务器接口都可以使用的.但是我最近着手一个关于数据可视化的项目,其接口全在WebService上,我们需要展示客户的各种数据并作出折线图饼状图.然而当我调用第一个接口时这套封装方法就遇到了问题.对于POST请求.
报错:Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unsupported media type (415)"
遇到这个问题后,各种找原因,百度上答案翻了一遍.最终问题确定在两个方面:
1,AFNetworking的支持解析类型是有限的,你需要在AFURLResponseSerialization.m里第227行处将
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];改为
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json", @"text/javascript", nil];
即增加它的可接受的类型.
2,在请求方法中,需要将发出的requestSerializer进行操作.而不是对responseSerializer进行操作.并直接用AFJSONRequestSerializer转化.在收到响应的方法体里也不再需要进行JSONObjectWithData:方法的解析.代码如下:(这一步很关键!)
#pragma mark -- POST请求 --
+ (void)postWithURLString:(NSString *)URLString parameters:(id)parameters success:(void (^)(NSDictionary * responseObject))success failure:(void (^)(NSError *error))failure{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
// NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
// success(dictionary);
success(responseObject);
}} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}}];
}
#pragma mark -- POST/GET网络请求 --
+ (void)requestWithURLString:(NSString *)URLString parameters:(id)parameters
type:(HttpRequestType)type success:(void (^)(NSDictionary * responseObject))success failure:(void (^)(NSError * error))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
// [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];//此句代码可注释.
switch (type) {
case HttpRequestTypeGet:
{
[manager GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
// NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
success(responseObject);
}} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}}];
}
break;
case HttpRequestTypePost:
{
[manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
// NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
// success(dictionary);
success(responseObject);
}} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}}];
}
break;
}
}
这样再进行post请求就会正常调用了.
附:
// [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];//此句代码可注释.
有人说可以在方法中加上这句话,大家可以试一下.
网络请求这一块看似简单,其实真的值得我们好好挖掘.里面的方法很多都还没有真正的理解到位.还需要慢慢的思考琢磨.我也相信,一个好的项目,一定是基于好的封装,简洁明了的体系.我们也都需要不断的进步和学习!