您的位置:首页 > 其它

AFNetworkingV2Client改进版

2014-01-24 16:29 417 查看
AFNetworkingV2Client
====================

一、AFHTTPClient是什么?

AFHTTPClient 是在AFNetworkingV2基础上做的改进,主要实现AFHTTPClientV2类,该类根据当前OS进行选择HTTP请求处理方式,
IOS7及其以后,采用AFHTTPSessionManager,IOS7之前采用AFHTTPRequestOperationManager。

二、改进:
1)在AFNetworkingV2中AFHTTPResponseSerializer属性增加@"text/html"
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
2)在AFHTTPSessionManager和AFHTTPRequestOperationManager类中增加header方法如下:
- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;

三、使用方法:

工程示例采用糗事百科的嫩草API
/*
- (void)loadData
{
[self.indicatorView startAnimating];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
[params setObject:@"5" forKey:@"count"];
[params setObject:@"1" forKey:@"page"];

__weak QiushiViewController *weakSelf = self;
[AFHTTPClientV2 requestWithBaseURLStr:@"http://m2.qiushibaike.com/article/list/latest" params:params httpMethod:HttpMethodGet successBlock:^(id responseObject) {

NSLog(@"GET responseObject: %@",responseObject);
QiushiViewController *strongSelf = weakSelf;
[strongSelf.indicatorView stopAnimating];

if ([responseObject isKindOfClass:[NSDictionary class]]) {
NSArray *array = [responseObject objectForKey:@"items"];
NSMutableArray *strollArray = [NSMutableArray arrayWithCapacity:[array count]];
for (int i = 0; i < [array count]; i++) {
NSDictionary *qiushiDic = [array objectAtIndex:i];
QiuShi *qs = [[QiuShi alloc] initWithQiuShiDictionary:qiushiDic];
[strollArray addObject:qs];
}

strongSelf.dataArray = strollArray;
}

} failedBlock:^(NSError *error) {
NSLog(@"GET error: %@",error);
QiushiViewController *strongSelf = weakSelf;
[strongSelf.indicatorView stopAnimating];
}];
}
*/

四、QA交流

本工程希望能给IOS开发者提供些帮助,同时更希望IOS同行,能提出宝贵意见,欢迎拍砖

项目gitHub地址:https://github.com/pjk1129/AFNetworkingV2Client
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: