AFNetworking报错:(415 Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unsupported media type (415)")

问题?

今天在与后台调接口的时候,遇到一个问题,使用AFNetworking报错,具体如下:

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unsupported media type (415)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f84d1409fa0> { URL: http://api.mutualtalk.net/api/common/123456 } { status code: 415, headers {

    "Content-Language" = en;

    "Content-Length" = 1048;

    "Content-Type" = "text/html;charset=utf-8";

    Date = "Tue, 05 Jul 2016 00:24:35 GMT";

    Server = "nginx/1.10.1";

} },--------一些二进制数据,此处省略n行字,

NSLocalizedDescription=Request failed: unsupported media type (415)

为什么会出现这个报错呢?

关于网络请求的接口,通常的做法是,利用第三方AFNetworking,这个强大的网络请求库,再此基础上,将项目中每个模块所用到的接口,进行一次简单的封装,这样,每个模块中的接口都是一类,便于调用,管理,维护...

先说一下我的做法,由于之前后台是php写的,但是我们的后台是java写的,都是搞server的,但是我不知道两个语言对数据格式的处理,有什么特殊的处理,反正套用之前的处理,报错,跟后台联调,后台根本接收不到请求,然后直接就报错,见这阵势,立马度娘神马,谷歌神马?stockoverflow什么的,各种答案,试了半天,然并卵,

以下为尝试的解决方法:

1>修改AFNetworking内部,这个文件AFURLResponseSerialization.m中修改代码就能解决:

第223行,初始化时,将如下代码:

1 self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil nil];  

修改为:

1 self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil nil];  

结果,运行,然并卵,我不否认,可能可以解决某些人遇到的问题,说是AFNetworking不支持以"text/html"格式的"content-type",反正我是添加了,没啥用;

2>在创建请求管理者(manger)是,做一些设置,其实本质上同第一种的解决方法是一样的,给AFNetworking添加一种支持的"text/html"格式,没什么用,还是报错,要不报400错;

   //1.创建一个请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
      //初始化响应者
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
     //添加一种支持的类型
   manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", nil];

    //2.发送请求
    NSDictionary *dict = @{
                           @"mobile":@"1111111",
                           @"type":@5,
                           @"Code":@"86",
                           @"key":@"eeqwerqwerqwerqwerqwe3af"
                   };
    NSString *url = @"http://api/asdfasd.com/334234";
     url = [url stringByRemovingPercentEncoding];

    [manager POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功%@",responseObject);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"%@",error);
        }
    }];

3>为了解决这个问题,单开一个项目,专门解决这个报错,由于java后台支持json数据格式传输,支持"application/json"格式的"content-type",后来在对请求数据格式和响应数据格式初始化的时候,将之前的父类换成了它的子类(AFJSONRequestSerializer);x-code7.3不提示(AFJSONRequestSerializer这个类),只有copy了,如下:

 1  //初始化响应者
 2 //    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
 3 //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
 4       manager.requestSerializer = [AFJSONRequestSerializer serializer];
 5       manager.responseSerializer = [AFJSONResponseSerializer serializer];
 6
 7 /***************    请忽略我---  *****************/
 8 /**
 9  `AFJSONRequestSerializer` is a subclass of `AFHTTPRequestSerializer` that encodes parameters as JSON using `NSJSONSerialization`, setting the `Content-Type` of the encoded request to `application/json`.
10  */
11 @interface AFJSONRequestSerializer : AFHTTPRequestSerializer
12 备注:
13 AFJSONRequestSerializer 继承自AFHTTPRequestSerializer,不过参数的编码形式,进行了设置,也就是设置 `Content-Type` 为 `application/json`,具体调到头文件,就是如下:
14 + (instancetype)serializer {
15     return [self serializerWithWritingOptions:(NSJSONWritingOptions)0];
16 }
17
18 + (instancetype)serializerWithWritingOptions:(NSJSONWritingOptions)writingOptions
19 {
20     AFJSONRequestSerializer *serializer = [[self alloc] init];
21     serializer.writingOptions = writingOptions;
22
23     return serializer;
24 }

至此,问题终于解决,到此,也只能怪自己学艺不精,吃一堑,长一智,慢慢积累吧,写给自己吧
时间: 2024-08-02 15:11:15

AFNetworking报错:(415 Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unsupported media type (415)")的相关文章

Error Domain=com.alamofire.error.serialization.response Code=-1016 &quot;Request failed: unacceptabl

在使用AFNetworking 2.0  的时候本来一切很顺畅,但是中途遇到几个比较坑的地方 这里分享一下爬坑经历,忘读者不能速爬坑! 在发送请求后,NSURLSessionDataTask一直报错 Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" AFURLResponseSerializat

[原]Error Domain=com.alamofire.error.serialization.response Code=-1016 &quot;Request failed: unacceptable con

转载请注明出处:http://blog.csdn.net/dengbin9009/article/details/43485617 在使用AFNetworking 2.0  的时候本来一切很顺畅,但是中途遇到几个比较坑的地方 这里分享一下爬坑经历,忘读者不能速爬坑! 在发送请求后,NSURLSessionDataTask一直报错 Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed:

Error Domain=com.alamofire.error.serialization.response Code=-1016 &quot;Request failed: unacceptable content-type: text/html&quot; 的问题原因及解决方案

Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html",此问题的原因就是使用的第三方框架AFNetworking 接口返回值类型不确定,由于服务器人员习惯于使用html文件所以将json文件也这么写了,导致没法解析 在模型类或者网络工具类中添加这句代码就能完美解决上述问题,建议不要直接修改AFNetw

Error Domain=com.alamofire.error.serialization.response Code=-1016 &quot;Request failed: unacceptable con

AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json",@"text/javascript",@"text/html",nil];

com.alamofire.error.serialization.response Code=-1016

在使用AFN访问网络的时候,报错com.alamofire.error.serialization.response Code=-1016. 这个问题是由于后台PHP的问题,请联系后台开发人员,添加 header('Content-type: application/json');

Unsupported Media Type 415问题解决办法(Ajax)

场景:Ajax传一个jsuon对象到服务器,让参数自动封装至与json对象匹配的java对象中. 错误类型 错误类型1: "status":415 "error":"Unsupported Media Type" "exception":"org.springframework.web.HttpMediaTypeNotSupportedException" 原因:没有使用JSON.stringify(row

AFNetworking 遇到错误 Code=-1016 &quot;Request failed: unacceptable content-type: text/plain&quot;

在开发过程使用了AFNetworking库,版本2.x,先运行第一个官方例子(替换GET 后面的url即可): AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operatio

cocoapods导入afnetworking报错问题

使用cocoapods导入afnetworking报错,原因是当前xcode的版本低使用了最新版本的afnetworking,就会出现如下错误,这时候只只要使用终端,更新使用低版本的afnetwork ing即可. 如果是其他情况下导致出现了APPLE   LLVM  6.0 Error 错误,可以尝试去资源库--Developer--xcode--DerivedData文件夾,删除 DerivedData文件夹下的内容(如DerivedData文件夹下无重要文件).如无法全部删除,则删除 De

【Unity3D】生成工程报错解决—UnityEditor.HostView:OnGUI() Error building Player: Couldn&#39;t build player because of unsupported data on target platform.

错误 错误1:An asset is marked as dont save, but is included in the build: unityEditor.HostView:OnGUI() 错误2:Building - Failed to write file: sharedassets0.assetsUnityEditor.HostView:OnGUI() 错误3:Error building Player: Couldn't build player because of unsup