标签:
AFN介绍
- AFN是一套操作网络的第三方框架
- NSURLConnection iOS2.0之后出现
- NSURLSession iOS7.0之后出现
- 默认支持序列化和反序列化json,xml数据需要自己解析
- 是一套优秀的错误处理机制
- 封装了Reachability
- 支持HTTPS(iOS9.0默认都是https网络协议,但可以在into.pilst中修改属性为所有协议都可以)
GET演示
url是字符串,如果有汉字和空格需要url编码
获取的数据直接进行了反序列化(如果获取的不是json数据在出错)
block的执行实在主线程上
Code:
//请求操作管理器--对NSURLConnection的封装
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
[manager GET:@"
http://127.0.0.1/index1.html"parameters:nilsuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@ %@",operation,responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"出错 %@",error);
}];
带参数的GET演示:
AFN会自动把字典转换成url后的参数加上 ?username=admin&password=123
//请求操作管理器--对NSURLConnection的封装
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
//get请求的参数
NSDictionary *params = @{@"username":@"admin",@"password":@"123"};
[manager GET:@"
http://127.0.0.1/php/login.php"parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@ ",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"出错 %@",error);
}];
POST演示
//请求操作管理器--对NSURLConnection的封装
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
//get请求的参数
NSDictionary *params = @{@"username":@"admin",@"password":@"123"};
[manager POST:@"
http://127.0.0.1/php/login.php"parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@ ",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"出错 %@",error);
}];
iOS的Serialization机制
AFHTTPRequestOperationManager初始化的方法
?self.requestSerializer = [AFHTTPRequestSerializer serializer];
请求数据的序列化
?self.responseSerializer = [AFJSONResponseSerializer serializer];
返回数据的序列化
•<AFURLRequestSerialization> 发送给服务器的数据
?AFHTTPRequestSerializer 二进制(默认)
?AFJSONRequestSerializer json
?AFPropertyListRequestSerializer plist
•<AFURLResponseSerialization> 从服务器接收的数据
?AFHTTPResponseSerializer 二进制
?AFJSONResponseSerializer json(默认)
?AFXMLParserResponseSerializer xmlParser(sax方式解析xml)
?AFXMLDocumentResponseSerializer dom解析xml (Mac OS X)
?AFPropertyListResponseSerializer plist
?AFImageResponseSerializer image
?AFCompoundResponseSerializer 组合
关于XML的两种解析方式:
SAX是把XML文件中的数据一点一点传递
DOM是把XML文件中的数据一次性加载,需要较大内存消耗
AFN类结构
1.AFN中的常用类
a.NSURLConnection
i.AFURLConnectionOperation
ii.AFHTTPRequestOperation
iii.AFHTTPRequestOperationManager
b.NSURLSession
i.AFURLSessionManager
ii.AFHTTPSessionManager
c.Reachability
d.Serialization
i.AFURLRequestSerialization 请求数据的序列化处理器
1.AFHTTPRequestSerializer
2.AFJSONRequestSerializer
3.AFPropertyListRequestSerializer
ii.AFURLResponseSerialization 响应数据的序列化处理器
1.AFHTTPResponseSerializer
2.AFJSONResponseSerializer
3.AFXMLParserResponseSerializer
4.AFXMLDocumentResponseSerializer
5.AFPropertyListResponseSerializer
6.AFImageResponseSerializer
7.AFCompoundResponseSerializer
Manager中的常用内容
AFHTTPRequestOperationManager
a.属性
i.baseURL
ii.requestSerializer
iii.responseSerializer
iv.operationQueue
v.reachabilityManager
b.方法
i.manager
ii.HTTPRequestOperationWithRequest
iii.GET
iv.HEAD
v.POST
vi.POST constructingBodyWithBlock formData
vii.PUT
viii.DELETE
iOS网络编程 AFNetwording框架的解析与使用
标签:
原文地址:http://www.cnblogs.com/arvink/p/5453786.html