zoukankan      html  css  js  c++  java
  • 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 }

    至此,问题终于解决,到此,也只能怪自己学艺不精,吃一堑,长一智,慢慢积累吧,写给自己吧

     文章首发:[个人博客](https://yizibi.github.io/)  

    [AFNetworking报错:415...](https://yizibi.github.io/2016/01/16/AFNetworking%E6%8A%A5%E9%94%99415/) 转载注明出处;

     

    好好做好分内之事,闲来之余,随便逛逛!
  • 相关阅读:
    天堂Lineage(單機版)從零開始架設教學 Installing Lineage 3.52 Server
    /dev/random vs /dev/urandom
    Linux Interactive Exploit Development with GDB and PEDA
    Python : Polymorphism
    Python : Data Encapsulation
    Using Keyboard Navigation
    Capture pictures using Jpython
    Java并发编程:volatile关键字解析
    java 资料收集
    解决ubuntu侧边栏固定应用单击无反应的问题
  • 原文地址:https://www.cnblogs.com/20150728-IOS/p/5642371.html
Copyright © 2011-2022 走看看