码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-10-15 01:08:54      阅读:1672      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   使用   ar   

今天写了一个快递查询的功能,使用的是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 = "\U7533\U901a";
        num = 768938779991;
        sign = "\U8349\U7b7e";
        state = 3;
    };
    errcode = 0;
    "track_data" =     {
        data =         (
                        {
                datetime = "2014-06-28 01:26:39";
                info = "\U5e7f\U4e1c\U4e1c\U839e\U864e\U95e8\U5317\U6805\U5206\U90e8 \U7684\U6536\U4ef6\U5458 \U7f57\U8fd0\U91d1\U5df2\U6536\U4ef6";
                place =                 (
                );
            },
                        {
                datetime = "2014-06-28 01:30:39";
                info = "\U7531 \U5e7f\U4e1c\U4e1c\U839e\U864e\U95e8\U5317\U6805\U5206\U90e8 \U53d1\U5f80 \U5e7f\U4e1c\U4e1c\U839e\U4e2d\U8f6c\U90e8";
                place = "\U5e7f\U4e1c\U4e1c\U839e";
            },
                        {
                datetime = "2014-06-28 04:21:17";
                info = "\U5e7f\U4e1c\U4e1c\U839e\U4e2d\U8f6c\U90e8 \U6b63\U5728\U8fdb\U884c \U88c5\U888b \U626b\U63cf";
                place =                 (
                );
            },
                        {
                datetime = "2014-06-28 04:21:17";
                info = "\U7531\U5e7f\U4e1c\U4e1c\U839e\U4e2d\U8f6c\U90e8 \U53d1\U5f80 \U56db\U5ddd\U5357\U5145\U822a\U7a7a\U90e8";
                place = "\U56db\U5ddd\U5357\U5145\U822a\U7a7a\U90e8";
            },
                        {
                datetime = "2014-06-29 01:23:43";
                info = "\U7531\U56db\U5ddd\U6210\U90fd\U822a\U7a7a\U90e8 \U53d1\U5f80 \U56db\U5ddd\U5357\U5145\U4e2d\U8f6c\U90e8";
                place = "\U56db\U5ddd\U5357\U5145";
            },
                        {
                datetime = "2014-06-29 06:26:36";
                info = "\U7531\U56db\U5ddd\U5357\U5145\U4e2d\U8f6c\U90e8 \U53d1\U5f80 \U56db\U5ddd\U5ba3\U6c49\U516c\U53f8";
                place = "\U56db\U5ddd\U5ba3\U6c49";
            },
                        {
                datetime = "2014-06-30 06:53:12";
                info = "\U5feb\U4ef6\U5df2\U5230\U8fbe \U56db\U5ddd\U5ba3\U6c49\U516c\U53f8";
                place = "\U56db\U5ddd\U5ba3\U6c49";
            },
                        {
                datetime = "2014-06-30 13:14:51";
                info = "\U56db\U5ddd\U5ba3\U6c49\U516c\U53f8 \U7684\U6d3e\U4ef6\U5458 \U7b26\U5148\U751f \U6b63\U5728\U6d3e\U4ef6";
                place = "\U56db\U5ddd\U5ba3\U6c49\U516c\U53f8";
            },
                        {
                datetime = "2014-06-30 18:13:48";
                info = "\U5df2\U7b7e\U6536,\U7b7e\U6536\U4eba\U662f\U8349\U7b7e";
                place =                 (
                );
            }
        );
    };
}

 

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



 

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

标签:des   style   blog   http   color   io   os   使用   ar   

原文地址:http://www.cnblogs.com/niit-soft-518/p/4025362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!