afnetworking
2015-06-09 11:15
316 查看
AFNetworking
AFNetworking为什么存在AFNetworking的功能
AFNetworking之HTTP
GET POST
AFNetworking功能封装
AFHTTPRequestOperationAFHTTPRequestOperation is a subclass of AFURLConnectionOperation for requests using the HTTP or HTTPS protocols. It encapsulates the concept of acceptable status codes and content types, which determine the success or failure of a request.
Although AFHTTPRequestOperationManager is usually the best way to go about making requests, AFHTTPRequestOperation can be used by itself.
GET with AFHTTPRequestOperation
NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.json"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request]; op.responseSerializer = [AFJSONResponseSerializer serializer]; [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; [[NSOperationQueue mainQueue] addOperation:op];
AFHTTPRequestOperationManager
AFHTTPRequestOperationManager encapsulates the common patterns of communicating with a web application over HTTP, including request creation, response serialization, network reachability monitoring, and security, as well as request operation management.
GET Request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }];
POST URL-Form-Encoded Request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *parameters = @{@"foo": @"bar"}; [manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }];
POST Multi-Part Request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *parameters = @{@"foo": @"bar"}; NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"]; [manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileURL:filePath name:@"image" error:nil]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }];
相关文章推荐
- 使用Docker做开发的建议团队工作流
- Ubuntu 14.04一步一步安装Openstack Kilo版本-7(keystone)
- java基础复习之四:注释,标识符,关键字, 变量,常量
- 数据挖掘分类算法之决策树(zz)
- Eclipse提示No java virtual machine(转载)
- iOS开发笔记-swift实现iOS数据持久化之归档NSKeyedArchiver
- cocos2dx吸蓝效果实现opengl绘制
- Android中Activity启动默认不显示输入法解决方法
- 编程中告警及日志级别说明
- @property 和@synthesize
- 页面有时无法输入中文的解决方案
- 系统管理员神器 monit 监控报警设置方法
- Yii2中的Ajax,异步更新表单
- 线段树(知识概念)
- 编程中告警及日志级别说明
- 公钥私钥加密
- Setup Ruby On Rails on Mac OS X 10.10 Yosemite
- Activity生命执行中的几个方法
- 04Top K算法问题
- C#:异步编程和线程的使用(.NET 4.5 )