zoukankan      html  css  js  c++  java
  • AFNetworking 遇到错误 Code=-1016 "Request failed: unacceptable content-type: text/plain"

    在开发过程使用了AFNetworking库,版本2.x,先运行第一个官方例子(替换GET 后面的url即可):

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    提示Error,但其实返回的是200 OK。
    Error: Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/plain"
    UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x100438a90>
    { URL: http://testadmin.test.com/webservice/test } { status code: 200, headers {
        Connection = "Keep-Alive";
        "Content-Encoding" = gzip;
        "Content-Length" = 61;
        "Content-Type" = "text/plain";
        Date = "Mon, 22 Feb 2016 09:19:32 GMT";
        "Keep-Alive" = "timeout=10, max=10000";
        Server = "SNMW1.0";
        Vary = "Accept-Encoding";
    } }, NSErrorFailingURLKey=http://testadmin.test.com/webservice/test,

    经过查资料,是默认的AFNetworking库,不支持解析 text/plain 类型,需要简单修改源代码就可以。
    打开AFURLResponseSerialization.m,找到类 AFJSONResponseSerializer,修改它的init函数:
    原内容:
    - (instancetype)init {
        self = [super init];
        if (!self) {
            return nil;
        }

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

    修改为:
    - (instancetype)init {
        self = [super init];
        if (!self) {
            return nil;
        }

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

    其实就是增加了text/plain,就可以了。
  • 相关阅读:
    poj 3415 后缀数组+单调栈
    hdu 3450 后缀数组
    hdu 2774 后缀数组
    后缀数组模板(倍增法)
    hdu 4405 概率dp
    zoj 3329 概率dp
    [日常摸鱼]bzoj2875[NOI2012]随机数生成器-矩阵快速幂
    [日常摸鱼]bzoj1038[ZJOI2008]瞭望塔-半平面交
    [日常摸鱼]bzoj1007[HNOI2008]水平可见直线-半平面交(对偶转凸包)
    [日常摸鱼]bzoj3083遥远的国度-树链剖分
  • 原文地址:https://www.cnblogs.com/xinghebuluo/p/5207738.html
Copyright © 2011-2022 走看看