AFN
2016-03-27 本文已影响375人
小盒盒
AFN
http://www.jianshu.com/p/047463a7ce9b
AFN简介
-
什么是AFN
-
全称是
AFNetworking
,是对NSURLConnection
的一层封装 -
虽然运行效率没有ASI高,但是使用比
ASI
简单 -
在
iOS
开发中,使用比较广泛 -
AFN
的github
地址
https://github.com/pokeb/AFNetworking/AFNetworking
AFN基本使用
-
AFHTTPSessionManager
- 是AFN中最重要的对象之一
- 封装了HTTP请求的常见处理
- GET\POST请求
- 解析服务器的响应数据
//创建 AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; //GET请求 - (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(id)parameters success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure //POST请求
-
(NSURLSessionDataTask *)POST:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
//文件上传
- (NSURLSessionDataTask *)POST:(NSString *)URLString
parameters:(id)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
```
- 网络监控
```objc
//网络监控
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
[manager startMonitoring];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"%d", status);
}];
提示:要监控网络连接状态,必须要先调用单例的startMonitoring方法
```
- GET
// AFHTTPSessionManager内部包装了NSURLSession
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
NSDictionary *params = @{
@"username" : @"xxx",
@"pwd" : @"xxx"
};
[mgr GET:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"请求成功---%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
- POST
// AFHTTPSessionManager内部包装了NSURLSession
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
NSDictionary *params = @{
@"username" : @"xxx",
@"pwd" : @"xxx"
};
[mgr POST:@"http://xxx/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"请求成功---%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
- 文件上传
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
[mgr POST:@"http://xxxx/upload" parameters:@{@"username" : @"123"}
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 在这个block中设置需要上传的文件
// NSData *data = [NSData dataWithContentsOfFile:@"/Users/wh/Desktop/placeholder.png"];
// [formData appendPartWithFileData:data name:@"file" fileName:@"test.png" mimeType:@"image/png"];
// [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/wh/Desktop/placeholder.png"] name:@"file" fileName:@"xxx.png" mimeType:@"image/png" error:nil];
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/wh/Desktop/placeholder.png"] name:@"file" error:nil];
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"-------%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];