标签:
众所周知,苹果搞的一套框架NSContention发送请求与接收请求的方式十分繁琐。操作起来很不方便。不仅要做区分各种请求设置各种不同的参数,而且还要经常在多线程里操作,同时还要对请求与返回的数据做各种序列化的操作,同时还要考虑请求数据的安全等一堆问题。
转载请注明出处:http://blog.csdn.net/xn4545945
一、早前的几个网络框架
1、ASI框架: HTTP终结者.很牛,
但是有BUG, 已经停止更新.
2、MKNetworkKit (印度人写的).
3、AFN一直还在更新.
AFNetworking的出现:MAC/iOS设计的一套网络框架.(为了简化网络操作)
*AFN专注与网络数据传输,以及网络中多线程的处理.
二、AFNetworking的使用
1、AFN特性 :
*登录传参数时,传递字典即可.(键名为参数名,键值为参数值).
*自动到子线程中执行,执行完后返回主线程.
*返回的结果自动序列化为NSDictionary.
2、使用AFN注意 :
*AFHTTPRequestOperationManager封装了通过HTTP协议与Web应用程序进行通讯的常用方法.(这个实例化的时候不是单例,
因为没有shared字)
*包括创建请求/响应序列化/网络监控/数据安全.
*方法等都是以AF开头的.
3、AFN能做的 (网络中的都涵盖了):
*GET/POST/PUT/DELETE/HEAD请求.
*JSON数据解析/Plist数据解析.(不支持XML数据解析)
*POSTJSON.
*上传/下载.
4、使用步骤
: (可参考说明文档)
1.首先需要实例化一个请求管理器AFHTTPRequestOperationManager.
2.设置请求的数据格式:默认是二进制.(不是可改)
*AFHTTPRequestSerializer(二进制)
*AFJSONRequestSerializer(JSON)
*AFPropertyListRequestSerializer(Plist)
3.设置响应的数据格式:默认是JSON.(不是可改)
*AFHTTPResponseSerializer(二进制)
*AFJSONResponseSerializer(JSON)
*AFPropertyListResponseSerializer(Plist)
*AFXMLParserResponseSerializer(XML)
*AFImageResponseSerializer(Image)
*AFCompoundResponseSerializer(组合的)
4.如果响应者的MIMEType不正确,就要修改acceptableContentTypes.
5.调用方法,发送响应的请求(GET/POST...).
关于修改AFN源码:通常序列化时做对text/plan等的支持时,可以一劳永逸的修改源代码,在acceptableContentTypes中修改即可。
AFN进行GET、POST登录:
- #pragma mark - get/post登录
- - (void)getLogin {
-
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- NSDictionary *dict = @{ @"username":@"xn", @"password":@"123" };
-
-
- [manager GET:@"http://localhost/login.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"GET --> %@, %@", responseObject, [NSThread currentThread]);
- } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
-
- - (void)postLogin {
-
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- NSDictionary *dict = @{ @"username":@"xn", @"password":@"123" };
-
-
- [manager POST:@"http://localhost/login.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"POST --> %@, %@", responseObject, [NSThread currentThread]);
- } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
AFN进行网络数据解析,获取Plist,JSON,XML(AFN不支持自动解析XML,有专门的框架去做,如SAX,PULL,KissXML等)
- #pragma mark - get 数据解析
- - (void)getJSON {
-
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- [manager GET:@"http://localhost/videos.json" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"%@", responseObject);
- } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
-
- - (void)getXML {
-
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
-
-
- [manager GET:@"http://localhost/videos.xml" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"%@", responseObject);
- } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
-
- - (void)getPlist {
-
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- manager.responseSerializer = [AFPropertyListResponseSerializer serializer];
- manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
-
-
- [manager GET:@"http://localhost/videos.plist" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"%@", responseObject);
- } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
用AFN来POST JSON数据,上传、下载等。(上传、下载主页说明上有https://github.com/AFNetworking/AFNetworking)
- #pragma mark - post json数据与上传文件等
- - (void)postJSON {
-
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- manager.requestSerializer = [AFJSONRequestSerializer serializer];
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
-
-
- NSDictionary *dict = @{ @"username":@"xn", @"password":@"123" };
-
-
- [manager POST:@"http://localhost/postjson.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {
- NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
- NSLog(@"%@",result);
- } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
iOS开发之AFNetworking网络编程
标签:
原文地址:http://www.cnblogs.com/ht-927/p/4329044.html