AFNetworking 学习

AFNnetworking快速教程,官方入门教程译

(2013-12-17 20:57:54)
 分类:常用框架

AFNetworking官网入门教程简单翻译,学习

AFNetworking 是一个能够快速使用的ios和mac osx下的网络框架,它是构建在Foundation URL LoadingSystem之上的,封装了网络的抽象层,可以方便的使用,AFNetworking是一个模块化架构,拥有丰富api的框架。


一、HTTP请求与操作:

1、AFHTTPRequestOperationManager:

该类封装与Web应用程序进行通信通过HTTP,包括要求制作,响应序列化,网络可达性监控和安全性,以及要求经营管理的常见模式。


GET 请求:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片

AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];

[manager GET:@"http://example.com/resources.json"parameters:nilsuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

   NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation,NSError *error) {

   NSLog(@"Error: %@", error);

}];


POST 带有表单参数的POST请求:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片

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?在CODE上查看代码片派生到我的代码片

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 formData) {

    [formDataappendPartWithFileURL: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对象必须实现, , , 协议


创建一个下载任务:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片

NSURLSessionConfiguration *configuration =[NSURLSessionConfiguration defaultSessionConfiguration];

AFURLSessionManager *manager = [[AFURLSessionManageralloc] initWithSessionConfiguration:configuration];


NSURL *URL =[NSURL URLWithString:@"http://example.com/upload"];

NSURLRequest *request =[NSURLRequest requestWithURL:URL];


NSURL *filePath = [NSURLfileURLWithPath:@"file://path/to/image.png"];

NSURLSessionUploadTask*uploadTask = [manager uploadTaskWithRequest:requestfromFile:filePath progress:nilcompletionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {

    if (error) {

       NSLog(@"Error: %@", error);

    } else {

       NSLog(@"Success: %@ %@", response, responseObject);

    }

}];

[uploadTask resume];


创建一个数据流任务:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片

NSURLSessionConfiguration *configuration =[NSURLSessionConfiguration defaultSessionConfiguration];

AFURLSessionManager *manager = [[AFURLSessionManageralloc] initWithSessionConfiguration:configuration];


NSURL *URL = [NSURLURLWithString:@"http://example.com/upload"];

NSURLRequest *request =[NSURLRequest requestWithURL:URL];


NSURLSessionDataTask*dataTask = [manager dataTaskWithRequest:requestcompletionHandler:^(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?在CODE上查看代码片派生到我的代码片

NSURL *URL = [NSURLURLWithString:@"http://example.com/resources/123.json"];

NSURLRequest *request =[NSURLRequest requestWithURL:URL];

AFHTTPRequestOperation *op = [[AFHTTPRequestOperationalloc] initWithRequest:request];

op.responseSerializer = [AFJSONResponseSerializerserializer];

[opsetCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responseObject) {

   NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation,NSError *error) {

   NSLog(@"Error: %@", error);

}];

[[NSOperationQueue mainQueue] addOperation:op];


连续操作多个:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片

NSMutableArray *mutableOperations = [NSMutableArrayarray];

for (NSURL *fileURLin filesToUpload) {

    NSURLRequest*request = [[AFHTTPRequestSerializer serializer]multipartFormRequestWithMethod:@"POST"URLString:@"http://example.com/upload"parameters:nilconstructingBodyWithBlock:^(id formData){

       [formDataappendPartWithFileURL:fileURL name:@"images[]" error:nil];

    }];

   

   AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];

   

   [mutableOperations addObject:operation];

}


NSArray *operations =[AFURLConnectionOperation batchOfRequestOperations:@[...]progressBlock:^(NSUInteger numberOfFinishedOperations, NSUIntegertotalNumberOfOperations) {

   NSLog(@"%lu of %lu complete",numberOfFinishedOperations, totalNumberOfOperations);

} completionBlock:^(NSArray *operations) {

   NSLog(@"All operations in batchcomplete");

}];

[[NSOperationQueue mainQueue]addOperations:operations waitUntilFinished:NO];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值