博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 使用AFNetworking
阅读量:4197 次
发布时间:2019-05-26

本文共 5006 字,大约阅读时间需要 16 分钟。

一下载: 
网址 -- 
二 环境:
 
 
需要引入的库 - CoreLocation.framework
 
 
SystemConfiguration.framework
 
 
MobileCoreServices.framework
 
 
Security.framework
 
 
需要在 ARC 的环境下 - 非 ARC 的工程中 - 请添加 -fobjc-arc 
三 结构:
1 : AFHTTPClient 
 
--  
 
提供了一个方便的网络交互接口,包括默认头,身份验证,是否连接到网络,批量处理操作,查询字符串参数序列化,已经多种表单请求
2 : AFHTTPRequestOperation 
-- 
和它得子类可以基于http状态和内容列下来区分是否成功请求了
3 : AFURLConnectionOperation
 
-- 
和它的子类继承NSOperation的,允许请求被取消,暂停/恢复和由NSOperationQueue进行管理。
4 : AFURLConnectionOperation
 
-- 
可以让你轻松得完成上传和下载,处理验证,监控上传和下载进度,控制的缓存。
四 使用 :
用法场景 1 :请求api数据
方法1 --
创建一个 AFHTTPClient 实例
AFHTTPClient * client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://api.jiepang.com"]];
// 这里一定要有一个 baseURL - 不然会抛出一个异常 -- 
创建一个 NSURLRequest 实例
NSDictionary * params = @{@"apiver": @"4",@"id" : @"830755858",@"extra_info": @"1"};
 
 
 
 
NSURLRequest * request = [client requestWithMethod:@"POST"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
path:@"users/show"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
parameters:params];
 
 
 
 
@param  
method 网络请求方式,比如 GET,POST,PUT,DELETE , 不能为 nil -- 
 
 
 
 
@param  
path  
 
和 baseURL 组合成为一个 url  
-- 通俗的说,就是我们调用的接口 -- 比如,我想调用http://api.jiepang.com/users/show,这个接口 -- 那么 baseURL 为 http://api.jiepang.com -- path 为users/show 
 
 
 
 
@param  
parameters 网络请求参数 -- http://api.jiepang.com/v1/users/show?id=123 -- id=123 就是这个参数
创建一个 AFHTTPRequestOperation 实例进行网络链接
AFHTTPRequestOperation * operation = [client HTTPRequestOperationWith
Request:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
NSLog(@"success obj == %@",responseObject);
 
 
 
 
 
 
 
 
 
 
 
 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
NSLog(@"faild , error == %@ ", error);
 
 
 
 
}];
 
 
 
 
[operation start];
 
 
 
 
这样 - 一次简单的调用就OK了 - 
 
 
 
 
当然也可以,用封装好的其它方法 -- 
 
 
 
 
- (void)getPath:(NSString *)path
 
 
 
 
 
parameters:(NSDictionary *)parameters
 
 
 
 
 
 
 
 
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
 
 
 
 
 
 
 
 
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
 
 
 
 
- (void)postPath:(NSString *)path
 
 
 
 
 
 
parameters:(NSDictionary *)parameters
 
 
 
 
 
 
 
 
 
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
 
 
 
 
 
 
 
 
 
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
 
 
 
 
- (void)putPath:(NSString *)path
 
 
 
 
 
parameters:(NSDictionary *)parameters
 
 
 
 
 
 
 
 
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
 
 
 
 
 
 
 
 
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
 
 
 
 
- (void)deletePath:(NSString *)path
 
 
 
 
 
 
 
 
parameters:(NSDictionary *)parameters
 
 
 
 
 
 
 
 
 
 
 
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
 
 
 
 
 
 
 
 
 
 
 
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
- (void)patchPath:(NSString *)path
parameters:(NSDictionary *)parameters
 
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
 
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
省去了组装 Resquest 的时间  
--
比如 - 
方法二 --
 
 
 
 
[client getPath:path
 
 
 
 
 
parameters:params
 
 
 
 
 
 
 
 
success:^(AFHTTPRequestOperation *operation, id responseObject) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
NSString * obj = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
NSLog(@"obj == %@",obj);
 
 
 
 
 
 
 
 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
 
 
 
 
 
 
 
 
 
 
 
 
NSLog(@"faild -- ");
 
 
 
 
 
 
 
 
}];
 
 
 
 
这和上面的效果是一样的 -- 
 
 
 
 
我们看到,前面返回的数据全部为 NSdata - 为我们添加了麻烦 -- 而且我们大部分的 api 返回数据都为 json -- 
 
 
 
 
同样 - 我们可以用 AFHTTPRequestOperation 的子类 AFJSONRequestOperation 来替代 -- 
 
 
 
 
方法三 -- 
 
 
 
 
 
 
 
NSDictionary * params = @{@"apiver": @"4",@"id" : @"830755858",@"extra_info": @"1"};
 
 
 
 
 
 
 
 
AFHTTPClient * client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://api.jiepang.com"]];
 
 
 
 
 
 
 
 
NSURLRequest * request = [client requestWithMethod:@"POST"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
path:@"users/show"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
parameters:params];
 
 
 
 
 
 
 
 
 
 
 
 
AFJSONRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWith
Request:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
NSLog(@"json == %@",JSON);
 
 
 
 
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
NSLog(@"faild -- ");
 
 
 
 
}];
 
 
 
 
 
 
 
 
[operation start];
 
 
 
 
使用场景 2 :异步加载图片 
 
 
 
 
AFImageRequestOperation 是继承自 AFHTTPRequestOperation -- 所以 - 方法大同小异 -- 
 
 
 
 
NSString * url = @"http://c.hiphotos.baidu.com/album/w=2048/sign=1d7ca85bac345982c58ae292
38cc30ad/f2deb48f8c5494ee7abe3336
2cf5e0fe99257e04.jpg";
 
 
 
 
// 这是一个大美女
 
 
 
 
 
 
 
 
创建 request
 
 
 
 
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cachePolicy:NSURLRequestReloadIgnori
ngLocalCacheData
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
timeoutInterval:30];
 
 
 
 
 
 
 
 
AFImageRequestOperation * operation = [AFImageRequestOperation imageRequestOperationWit
hRequest:request
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
imageProcessingBlock:^UIImage *(UIImage *image) {
 
 
 
 
 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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
NSLog(@"reload image faild , error == %@ ",error);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
}];
 
 
 
 
 
 
 
 
[operation start];
 
 
 
 
这个方法里有三个block ,success 和 failure 不说 -- processimageBlock -- 是在 图片加载完后,对 图片 处理的 block ,可以为 nil ,在 success 之前调用  
--

转载地址:http://vyzli.baihongyu.com/

你可能感兴趣的文章
JMeter性能测试入门
查看>>
JMeter最常用的三种类型的压力测试
查看>>
Hibernate HQL 语法大全(上)
查看>>
Hibernate HQL 语法大全(下)
查看>>
深入Java事务的原理与应用
查看>>
JTA(XA)原理解析
查看>>
UltraEdit中使用正则表达式
查看>>
CSS单位和CSS默认值大全
查看>>
CSS核心--盒子模型
查看>>
Drools业务逻辑框架
查看>>
150天成为JAVA高级程序员?
查看>>
深入理解JAVA事件机制
查看>>
深入DNS域名解析服务原理
查看>>
CMMI的含义及重点
查看>>
HQL中In的问题详解
查看>>
自动化测试工具selenium使用介绍
查看>>
XPath实例教程
查看>>
Hibernate锁机制--悲观锁和乐观锁
查看>>
Tomcat、Websphere和Jboss类加载机制
查看>>
程序员如何缓解“电脑病”
查看>>