標籤:url complete init sha 網路請求 htm object tle 編碼
在進行網路請求時出現-1016 是因為只支援
text/json,application/json,text/JavaScript
你可以添加text/html
一勞永逸的方法是 在
AFURLResponseSerialization.h
裡面搜尋
self.acceptableContentTypes
然後 在裡面 添加
@"text/html",@"text/plain"
這樣就可以解決-1016的錯誤了
但是隨之而來的是3840錯誤
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=0x9152780 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
你會發現出現此錯誤
怎麼辦呢
添加如下語句 就可以解決問題了
manger.requestSerializer = [AFHTTPRequestSerializerserializer];
manger.responseSerializer = [AFHTTPResponseSerializerserializer];
是否成功了,成功了吧!但是新問題出現了 編碼問題 如果伺服器返回a 的話 你收到的 是<61>
這樣 怎麼能行呢?
當你用瀏覽器 去請求時 發現 回應標頭Content-Type: text/html;charset=UTF-8 是這樣的
但是afNetwork 請求是Content-Type:text/plain;charset=ISO-8859-1 是這樣的 不一致了吧
為什麼 pc瀏覽器 訪問的 和用afNetwork 訪問的 不一致呢? 不瞭解 什麼情況?
接著發現 其實 添加 如下二句 即可 也不用去修改AFURLResponseSerialization.h 裡面的東西
manger.requestSerializer = [AFHTTPRequestSerializerserializer];
manger.responseSerializer = [AFHTTPResponseSerializerserializer];
把 收到的
responseObject 轉換一下 編碼 就OK了
NSData *doubi = responseObject;
NSString *shabi = [[NSString alloc]initWithData:doubi encoding:NSUTF8StringEncoding];
AFNetwork 2.0在請求時報錯code=-1016 和 3840