afnetworking支持gzip是不是不支持ios8.3

iOS开发 AFNetworking 3.0使用遇到的问题补充
1.很多初学者,在每处用到网络请求的地方会直接拿afn实例去请求,从而带来了后续难以维护代码的问题
[[AFHTTPSessionManager manager] POST:nil parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
} failure:^(NSURLSessionDataTask *task, NSError *error) {
思考:如果afn不更新了,后续出现重大bug适配问题,从而导致每处用到afn的地方都要修改,请问怎么办?
答案:呵呵,只能一个个修改了!有人会说,怎么可能不更新,很多人用着呢,怕什么。那假设其他第三方也同样出现类型的问题呢?所以是不是应该有方法或某种思想去解决呢?
解决方法:写个网络基类HRHttpBase,提供两个方法:GET 与 POST,外面请求时,直接调用这两个方法,万一afn出了问题,只要修改这两个方法即可,从而减轻后续维护成本!这种做法同样适用于其他的第三方库, 这也是一种开发思想。
HRHttpBase.h
HRHttpBase.m
假设业务复杂的话,如登录,涉及到第三方登录之类的,有比较多的网络请求,可以专门抽一个网络请求类要处理:
HRhttpLogin.h
HRhttpLogin.m
2.用到CocoaPods,但更改了afn代码问题
这是缺少text/html解析格式,请求网络时,只要添加上就行了,很多人在网上找到设置的地方(如下图)。但他忘了,项目用CocoaPods管理第三方库的,如果项目上线之前,同事更新了CocoaPods那么之前写的代码就没了!
解决办法:通过AFHTTPSessionManager实例找到AFHTTPResponseSerializer 对象,然后看有没提供对应的方法或属性可设置该格式。
像类似的网络请求header和user-agent都应该在自己的代码里设置!
3.返回字符串而并非是json或xml格式,怎么解析?
开发中确实是存在奇葩的需求,还有可能返回一堆html格式的String,要截取Sting里的某个key。对于处理不了的格式,afn有可能error回调。
用AFHTTPSessionManager,我没找到未解析的字符(知道的朋友可以告诉我,谢谢)。最后还是用了AFHTTPRequestOperationManager
operation.responseString即为未格式化的字符
Paste_Image.png
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'使用AFNetworking框架遇到的一个经典bug的解决方案 - 简书
使用AFNetworking框架遇到的一个经典bug的解决方案
以上为个人愚见, 如有不妥,望大家斧正!!!本文的GitHub源码下载地址:
如需转载,请注明转载自的GitHub项目
在获取网络数据的时候, 我们一般会使用到一个非常著名的框架: AFNetworking框架, 可以说,这是作为iOS攻城狮必备的框架之一;这个框架是非常强大的一个框架, 对于发送异步请求来说,简直没有比这个更好用的了, 不过,在使用的过程中,我们可能会遇到这样一个bug: 如下
连接出错 Error Domain=com.alamofire.error.serialization.response Code=-1016
"Request failed: unacceptable content-type: text/html" UserInfo=
{com.alamofire.serialization.response.error.response=&NSHTTPURLResponse: 0x7f93fad1c4b0&
{ URL: http://c./nc/article/headline/T3/0-140.html }
{ status code: 200, headers { .....}
f2 3a226e65 f62 d631 362d320 ad7d&,
NSLocalizedDescription=Request failed: unacceptable content-type: text/html}
由于数据很多,所以返回的请求体,和响应体部分我用省略号(......)代替了, 但是,通过上面的返回的信息,我们不难看出,状态码是200, 而且也有一堆数据, 但是在tableviewCell中就是没有显示, 在最后的时候还出现"NSLocalizedDescription=Request failed: unacceptable content-type: text/html} " 这样一句话;
分析:那么这个错误是什么原因造成的呢?
通过这句话:unacceptable content-type: text/html,我们可以看出报错原因:是不接收的内容类型,也就是说AFNetworking框架不支持解析text/html这种格式. 那么怎样解决呢?
首先我们需要明白: AFNetworking为什么能够解析服务器返回的东西呢?
因为manager有一个responseSerializer属性.它只设置了一些固定的解析格式.其中不包含text/html这种数据的格式.因为解析报错了.我们来看一下AFNetworking解析格式的底层:
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
通过底层,我们也可以看见,确实是没有text/html这种数据的格式的,
那如何解决这个问题呢?
错误的解决方法下面我尝试了三种方法:
解决方法1:
直接给acceptableContentTypes属性添加类型
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManagermanager];
// 添加 text/html 类型到可接收内容类型中
mgr.responseSerializer.acceptableContentTypes= [NSSetsetWithObjects:@"text/html", nil];
通过执行结果可以很明显的看得出,我们已经非常成功的获取到了数据;
对方法1的思考:
首先,我们可以明显的看出,方法1确实是可以解决问题的,但是这样解决真的好吗?
不一定!为什么呢? 很简单, 如果我们只是发送一条网络请求,无疑方法1是最恰当的解决方案了,
但是实际开发中,我们不可能只发一次请求, 那么就需要我们每次发请求的时候都来写一次这些代码; 当然,如果您愿意写,那我也没办法多说什么了;很显然,这个方法是存在不足的!
于是我们有了第二种方法:
解决办法2: 直接到框架的源代码中添加类型
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
self.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json" ,@"text/javascript", nil];
分析方法2:
不得不说,这也是一种办法, 而且釜底抽薪,效果方面呢,当然也是显而易见了, 但是, 注意了,这个方法2 又真的恰当吗? 真的好吗?
我们来假设一种情况, 而且实际开发中必然会发生的情况: 这个框架更新了!!!对,就是更新了!!!
更新了显然又会回到之前的状态傻眼了吧?
实际开发中,我们都会用cocoaPods来管理我们的第三方框架,
当某个框架更新之后, cocoaPods会下载最新的框架源码镶嵌到我们的项目中, 我们并不能保证AFNetworking这个框架一定会把我们需要的类型添加上去, 所以每一次更新,我们都需要针对源码再做一次修改很显然,这也是费力不讨好的;
那么有没有一劳永逸的方法呢?
别急,马上就来!!!
解决办法3: 自定义一个manager ,拓展一个类型
&&这里需要考虑到两种情况: 如下
&1. 如果你的APP只需要适配iOS7.0之前的版本,为了能够适配旧系统,需要使用 AFHTTPRequestOperationManager
&2. 如果你的APP只需要适配iOS7.0之后的版本,那么你需要 自定义的类是继承AFHTTPSessionManager的
这里我只简单的介绍iOS7.0之后的版本,
自定义manager,继承自AFHTTPSessionManager
@interface DXHTTPManager : AFHTTPSessionManager
在.m文件中,重写父类的manager方法
目的 : 添加类型
+ (instancetype)manager {
DXHTTPManager *mgr = [super manager];
// 创建NSMutableSet对象
NSMutableSet *newSet = [NSMutableSet set];
// 添加我们需要的类型
newSet.set = mgr.responseSerializer.acceptableContentT
[newSet addObject:@"text/html"];
// 重写给 acceptableContentTypes赋值
mgr.responseSerializer.acceptableContentTypes = newS
在发送请求的时候,使用我们自定义的类来发送请求
[[DXHTTPManager manager] GET:@"http://...."
parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary * responseObject)
NSLog(@"请求成功 -- %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"请求失败 -- %@",error);
方法3执行结果:
1. 使用方法3来解决这个bug,虽然看似比第一种,第二种要繁琐一些,实则可拓展性,和维护方面,要好得多,以后我们开发项目的时候, 只需要将我们自定义的这个类拖进去就可以了, 假如有需要新的类型的时候, 也只是简单的多配置一下类型即可, 2.此外方法3还有一个好处,我们假设一个情景: 如果有一天我们使用的这个AFNetworking框架不再更新了,甚至作废了呢? 这种可能性还是有的,如果我们用方法1和方法2的话, 那个时候你会在项目中看见一堆一堆的bug,而如果我们使用的方法3的话,那么只需要简单的将继承的类更换成我们最新使用的框架里面的类,然后做一些简单的配置即可,这样维护起来的成本自然就轻松了,3.而这也正是我们代码重构, 和优化项目架构的思路之一!!!Does AFNetworking 2.0 support iOS 6.0? - Stack Overflow
to customize your list.
Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
J it only takes a minute:
AFNetworking 2.0 was announced last week. But I find different requirements for AFNetworking 2.0.
tells it requires either iOS 7.0 and above, while
tells it requires either iOS 6.0 and above.
Dose AFNetworking 2.0 support iOS 6.0?
44.8k1290119
Accoding to , you can simply use AFN 2.0 but not include the NSURLSession related files.
For example if you use CocoaPods (if you don't, you should really consider doing it, it's awesome!), simply use this on your Podfile to get AFN 2.0 without NSURLSession-related features:
pod 'AFNetworking', '~& 2.0'
This will only get the "Core" subspec, which supports the deployment target 6.0 for iOS (s.ios.deployment_target = '6.0' in the podspec) and does not integrate NSURLSession-related files. So this will be compatible with your project using an iOS6 or greater deployment target.
If you later wish to drop the 6.0 support on your project and take advantage of the NSURLSession capabilities, you can add the NSURLSession subspec to your Podfile:
pod 'AFNetworking', '~& 2.0' # Core, without NSURLSession, compatible with iOS6
pod 'AFNetworking/NSURLSession', '~& 2.0' # Add NSURLSession features, only compatible with iOS7
You can see in the podspec that adding the NSURLSession subspec will add the s.ios.deployment_target = '7.0' requirement as a consequence, requiring your project's deployment target to be 7.0 or greater too of course, as you could expect.
[EDIT] Since my answer, Mattt updated its code and podspec so that you can get the NSURLSession subspec even when targeting iOS6. See
(and other ones related).
So now you can get the NSURLSession subspec even for you iOS6-min projects, and just conditionally call them at runtime only if ([NSURLSession class]) is true.
29.1k66770
AFNetworking 2.0 is supported in iOS 6. My problem was that I had previously installed it with the platform set to iOS 7. I fixed it by doing this:
Deleted everything from my Podfile
Ran pod install, which deleted all my pods.
Added everything back to my Podfile, with platform :ios, '6.0'
And I was able to still use my existing AFNetworking code with no problems.
8,09063660
Yes, it does support iOS6, except for the NSURLSessionTask parts, which are iOS7 only.
I was confused as well, but it looks during the transition from 1.x -> 2.0 not all the docs had yet been updated. They have been now, and it works just fine in my project that supports iOS6.
1,12211232
You can use AFNetworking 2.0 .Just have to ensure that the deployment target is set to 6.0 while installing using cocoa pods.I had initially installed AFNetworking when my deployment target was 7.0 .So had to remove it from cocoa pods set deployment target to 6.0 and then do install again.
I didn't use pod at all. Instead, I downloaded it and dragged the files to the project, and then encountered this problem as well. To fix it I simply deleted the AFURLSessionManager and AFHTTPSessionManager files from the project. Then it worked.
6,07083780
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .24625
Stack Overflow works best with JavaScript enabledAFNetworking框架使用浅析_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
AFNetworking框架使用浅析
||文档简介
无限互联3G学院|
总评分0.0|
&&A​F​N​e​t​w​o​r​k​i​n​g​框​架​使​用​浅​析
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩2页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢

我要回帖

更多关于 afnetworking支持gzip 的文章

 

随机推荐