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

今天寫了一個快遞查詢的功能,使用的是AFNetWorking框架,javascript

貼個代碼:html

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,可是寫後臺那個鳥非要寫:java

) 把返回的內容類型寫成"text/html",由於 AFNetWorking框架是解析不了這種類型的,因此就會報錯,這時候抱怨那老鳥是沒用的,難道你想拿板磚拍他麼~仍是好好改你的代碼吧!
若是規範寫,應該這樣寫: response.setContentType("text/html"); response.getWriter().write(json)

For JSON:json

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

For JSON-P:api

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

因此,咱們必需要設置咱們的解析器:
self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

加上這句話,app

這時候運行,若是你的請求方式是post,那我就呵呵了,我也是這樣,後來報錯,苦逼~框架

錯誤代碼:async

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,那麼問題來了,到底對不對呢?恭喜本身,真的對了~ide

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 =                 (
                );
            }
        );
    };
}

 

若是有大神知道那段英文啥意思,麻煩留言下。post

相關文章
相關標籤/搜索