首页 > 代码库 > AFNnetworking快速教程,官方入门教程译
AFNnetworking快速教程,官方入门教程译
AFNnetworking快速教程,官方入门教程译
分类: IOS2013-12-15 20:29 12489人阅读 评论(5) 收藏 举报
afnetworkingjsonios入门教程快速教程
AFNetworking官网入门教程简单翻译,学习
AFNetworking 是一个能够快速使用的ios和mac os x下的网络框架,它是构建在Foundation URL Loading System之上的,封装了网络的抽象层,可以方便的使用,AFNetworking是一个模块化架构,拥有丰富api的框架。
一、HTTP请求与操作:
1、AFHTTPRequestOperationManager:
该类封装与Web应用程序进行通信通过HTTP,包括要求制作,响应序列化,网络可达性监控和安全性,以及要求经营管理的常见模式。
GET 请求:
[objc] view plaincopyprint?
- 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 带有表单参数的POST请求:
[objc] view plaincopyprint?
- 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格式的表单文件上传请求:
[objc] view plaincopyprint?
- 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);
- }];
二、Session管理:
1、AFURLSessionManager:创建和管理制定的NSURLSession对象
2、NSURLSessionConfiguration对象必须实现<NSURLSessionTaskDelegate>, <NSURLSessionDataDelegate>, <NSURLSessionDownloadDelegate>, <NSURLSessionDelegate>协议
创建一个下载任务:
[objc] view plaincopyprint?
- NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
- AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
- NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
- NSURLRequest *request = [NSURLRequest requestWithURL:URL];
- NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
- NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
- if (error) {
- NSLog(@"Error: %@", error);
- } else {
- NSLog(@"Success: %@ %@", response, responseObject);
- }
- }];
- [uploadTask resume];
创建一个数据流任务:
[objc] view plaincopyprint?
- NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
- AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
- NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
- NSURLRequest *request = [NSURLRequest requestWithURL:URL];
- NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
- if (error) {
- NSLog(@"Error: %@", error);
- } else {
- NSLog(@"%@ %@", response, responseObject);
- }
- }];
- [dataTask resume];
四、使用AFHTTPRequestOperation
1、AFHTTPRequestOperation是使用HTTP或HTTPS协议的AFURLConnectionOperation的子类。
它封装的获取后的HTTP状态和类型将决定请求的成功与否。
2、虽然AFHTTPRequestOperationManager通常是最好的去请求的方式,但是AFHTTPRequestOpersion也能够单独使用。
通过GET方式:
[objc] view plaincopyprint?
- 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];
连续操作多个:
[objc] view plaincopyprint?
- NSMutableArray *mutableOperations = [NSMutableArray array];
- for (NSURL *fileURL in filesToUpload) {
- NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
- [formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];
- }];
- AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
- [mutableOperations addObject:operation];
- }
- NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:@[...] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
- NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);
- } completionBlock:^(NSArray *operations) {
- NSLog(@"All operations in batch complete");
- }];
- [[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];
AFNnetworking快速教程,官方入门教程译
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。