AFNetworkingV2Client改进版

AFNetworkingV2Client

====================


一、AFHTTPClient是什么?


AFHTTPClient 是在AFNetworkingV2基础上做的改进,主要实现AFHTTPClientV2类,该类根据当前OS进行选择HTTP请求处理方式,

IOS7及其以后,采用AFHTTPSessionManagerIOS7之前采用AFHTTPRequestOperationManager


二、改进:

1)在AFNetworkingV2AFHTTPResponseSerializer属性增加@"text/html"

self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

2)在AFHTTPSessionManagerAFHTTPRequestOperationManager类中增加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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。