• AFNetWorking Request failed: unacceptable content-type: text/html


    今天写了一个快递查询的功能,使用的是AFNetWorking框架,

    贴个代码:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
            NSDictionary *dict=@{@"key": @"key",@"com": @"shentong",@"cno": @"768938779991"};
            NSString* encodedString = [@"http://api.kuaidiwo.cn:88/api/" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [appdelegate.manager POST:encodedString parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSLog(@"%@",responseObject);
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"%@",error);
            }];
        });

    红色的是请求链接,好了,按说是没问题(但是如果别人写后台不规范,明明传回来的是json,但是写后台那个鸟非要写:

     response.setContentType("text/html");
      response.getWriter().write(json)) 把返回的内容类型写成"text/html",因为 AFNetWorking框架是解析不了这种类型的,所以就会报错,这时候抱怨那老鸟是没用的,难道你想拿板砖拍他么~还是好好改你的代码吧!
    如果规范写,应该这样写:

    For JSON:

    Content-Type: application/json
     response.setContentType(application/json");

    For JSON-P:

    Content-Type: application/javascript
    请参考:
    http://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type

    所以,我们必须要设置我们的解析器:
    self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

    加上这句话,

    这时候运行,如果你的请求方式是post,那我就呵呵了,我也是这样,后来报错,苦逼~

    错误代码:

    2014-10-14 22:43:32.953 测试快递[5208:60b] Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8c9a410 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
    

    原谅我英语那么差,我也不知道问题错哪了,调了一下午,无意间把post改称get,那么问题来了,到底对不对呢?恭喜自己,真的对了~

    014-10-14 22:45:10.135 测试快递[5229:60b] {
        "ems_info" =     {
            adate = "2014-06-30 18:13";
            des =         (
            );
            kind = "U7533U901a";
            num = 768938779991;
            sign = "U8349U7b7e";
            state = 3;
        };
        errcode = 0;
        "track_data" =     {
            data =         (
                            {
                    datetime = "2014-06-28 01:26:39";
                    info = "U5e7fU4e1cU4e1cU839eU864eU95e8U5317U6805U5206U90e8 U7684U6536U4ef6U5458 U7f57U8fd0U91d1U5df2U6536U4ef6";
                    place =                 (
                    );
                },
                            {
                    datetime = "2014-06-28 01:30:39";
                    info = "U7531 U5e7fU4e1cU4e1cU839eU864eU95e8U5317U6805U5206U90e8 U53d1U5f80 U5e7fU4e1cU4e1cU839eU4e2dU8f6cU90e8";
                    place = "U5e7fU4e1cU4e1cU839e";
                },
                            {
                    datetime = "2014-06-28 04:21:17";
                    info = "U5e7fU4e1cU4e1cU839eU4e2dU8f6cU90e8 U6b63U5728U8fdbU884c U88c5U888b U626bU63cf";
                    place =                 (
                    );
                },
                            {
                    datetime = "2014-06-28 04:21:17";
                    info = "U7531U5e7fU4e1cU4e1cU839eU4e2dU8f6cU90e8 U53d1U5f80 U56dbU5dddU5357U5145U822aU7a7aU90e8";
                    place = "U56dbU5dddU5357U5145U822aU7a7aU90e8";
                },
                            {
                    datetime = "2014-06-29 01:23:43";
                    info = "U7531U56dbU5dddU6210U90fdU822aU7a7aU90e8 U53d1U5f80 U56dbU5dddU5357U5145U4e2dU8f6cU90e8";
                    place = "U56dbU5dddU5357U5145";
                },
                            {
                    datetime = "2014-06-29 06:26:36";
                    info = "U7531U56dbU5dddU5357U5145U4e2dU8f6cU90e8 U53d1U5f80 U56dbU5dddU5ba3U6c49U516cU53f8";
                    place = "U56dbU5dddU5ba3U6c49";
                },
                            {
                    datetime = "2014-06-30 06:53:12";
                    info = "U5febU4ef6U5df2U5230U8fbe U56dbU5dddU5ba3U6c49U516cU53f8";
                    place = "U56dbU5dddU5ba3U6c49";
                },
                            {
                    datetime = "2014-06-30 13:14:51";
                    info = "U56dbU5dddU5ba3U6c49U516cU53f8 U7684U6d3eU4ef6U5458 U7b26U5148U751f U6b63U5728U6d3eU4ef6";
                    place = "U56dbU5dddU5ba3U6c49U516cU53f8";
                },
                            {
                    datetime = "2014-06-30 18:13:48";
                    info = "U5df2U7b7eU6536,U7b7eU6536U4ebaU662fU8349U7b7e";
                    place =                 (
                    );
                }
            );
        };
    }
    

    如果有大神知道那段英文啥意思,麻烦留言下。



  • 相关阅读:
    Linux守护进程
    sequel pro无法连接mysql服务器
    socket编程之并发回射服务器2
    Unix的I/O模型
    nginx.conf laravel 配置
    phpstudy使用PHP+nginx配置Laravel
    nginx配置文件分开配置
    centos安装composer
    linux下 设置php的环境变量 php: command not found
    laravel 安装
  • 原文地址:https://www.cnblogs.com/niit-soft-518/p/4025362.html
Copyright © 2020-2023  润新知