iOS 使用AFNetworking遇到错误 Error Domain=com.alamofire.error.serialization.response Code=-1016
2018-01-11 08:47:04
错误日志:
Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f999be478d0> { URL: myUrlXXXXXX } { status code: 200, headers {
** "Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";**
** Connection = "keep-alive";**
** "Content-Encoding" = gzip;**
** "Content-Type" = "text/html; charset=utf-8";**
** Date = "Tue, 24 Nov 2015 09:06:04 GMT";**
** Expires = "Thu, 19 Nov 1981 08:52:00 GMT";**
** Pragma = "no-cache";**
** Server = "nginx/1.1.19";**
** "Set-Cookie" = "PHPSESSID=qdvtek1k91oeva2u8fats39l93; path=/";**
** "Transfer-Encoding" = Identity;**
** "X-Powered-By" = "PHP/5.3.10-1ubuntu3.21";**
} }, NSErrorFailingURLKey=http:
URL: myUrlXXXXXX
, com.alamofire.serialization.response.error.data=<7b226572 726f725f 636f6465 223a302c 22657272 6f725f6d 7367223a 22222c22 75706c6f 61645f75 726c223a 22687474 703a5c2f 5c2f7777 772e7467 7370792e 636f6d5c 2f6c6f6f 70657273 5c2f7365 72766572 5c2f6269 6e5c2f63 7573746f 6d65725c 2f75706c 6f61645c 2f373332 31746f75 7869616e 672e6a70 67227d>, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}
AFJSONResponseSerializer.m中,222行左右
把 这句: self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
在进行网络请求时出现-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];
Printing description of
error
:
Error
Domain
=NSURL
Error
Domain
Code
=
-
999 "(null)"
出现
错误
的原因是因为webview在之前的请求还没有加载完成,下一个请求开始发起了,因此webview会取消掉之前的请求,便会回调到didFailLoadWith
Error
。
-
(void)webView:(UIWebView *)...
主要是AF的问题 解析格式不全,
Code
=
-
1016 往往会在最后报
只要加上这段代码 补全解析格式即可manager.
response
Serializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json",nil];
错误
日志:
Error
Domain
=
com
.
alamofire
.
error
.
serialization
.
response
Code
=
-
1016 "Request failed: unacceptable content
-
type: text/html" UserInfo={
com
.
alamofire
.
serialization
.
response
.
error
.
response
=&...
以Vue提供了一个内置组件keep
-
alive来缓存组件内部状态,避免重新渲染。(在开发Vue项目的时候,有一部分部分组件是没必要多次渲染的)
keep
-
alive属性:include
-
字符串或正则表达式。只有匹配的组件会被缓存。exclude
-
字符串或正则表达式。任何匹配的组件都不会被缓存。
缓存动态组件:
<keep
-
alive>包裹动态组件时,会缓存不活动...
http网络库是集XML解析,Json解析,网络图片下载,plist解析,数据流请求操作,上传,下载,缓存等网络众多功能于一身的强大的类库。最新版本支持session,xctool单元测试。网络获取数据一直是手机软件的重中之重,如果处理的不好,会造成很差的用户体验。随着ASIHTTPRequest的停止更新,更换网络库是必然的事情,
AFNetworking
就是很好的替代品。而且都是轻量级,不要担心
1.URL的参数不对(和后台的对应不上)
2.URL的路径不对(和后台沟通后发现后台接口文档路径不对)
3.
AFNetworking
问题: 修改acceptableContentTyp...
iOS
错误
:
Error
Domain
=NSCocoa
Error
Domain
Code
=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.}
今天突然发现有些窗口请求失败,赶紧排查修复,
一开始就想到是后台的问题,可是安卓说没问题,有数据接收
然后自己看输出日记
发现来到
AFNetworking
后就跳出
has been blocked by CORS policy: No 'Access
-
Control
-
Allow
-
Origin' header is present on the requested resource.
Referrer Policy:no
-
referrer
-
when
-
downgrade
使用
Alamofire
发了一个GET请求,出现了如下
错误
:
response
Serialization
Failed(reason:
Alamofire
.AF
Error
.
Response
Serialization
FailureReason.json
Serialization
Failed(
error
:
Error
Domain
=NSCocoa
Error
Domain
Code
=3840 "Invalid value around character 4104." UserInfo={NSDebugDescript
IOS
网络访问之
使用
AFNetworking
AFNetworking
是
IOS
上常用的第三方网络访问库,我们可以在github上下载它,同时github上有它详细的
使用
说明,最新的AFNetworing2.0与1.0有很大的变化,这里仅对2.0常用的
使用
方法进行总结
基于NSURLConnection的API
提交GET请求
AFHTTPRequestOpe
NSDictionary *erroInfo =
error
.userInfo;
NSData *data = [erroInfo valueForKey:@“
com
.
alamofire
.
serialization
.
response
.
error
.data”];
NSString *
error
String = [[NSString alloc] initWit
在
使用
AFNetworking
2.0 的时候本来一切很顺畅,但是中途
遇到
几个比较坑的地方
这里分享一下爬坑经历,忘读者不能速爬坑!
在发送请求后,NSURLSessionDataTask一直报错
Error
Domain
=
com
.
alamofire
.
error
.
serialization
.
response
Code
=
-
1016 "Request failed: unacceptable
Domain
=
com
.
alamofire
.
error
.
serialization
.
response
Code
=
-
1011 "Request failed: not found (404)"
如图日志我们并不陌生,当我们用关键字在网上找答案时,不难发现很多
Code
r 都会跟我们说
afnetworking
的
serialization
对象设置一下,把content type的application/json加进去。认为这是解析返回数据的时候,设置不对造成的。但是往往我们设置了 ,
错误
日志依旧
所以当我们的日志报以下
错误
时,不单要看
afnetworking
设置是否,我们还需要注意是不是我