首页 > 代码库 > iOS AFNetworking的使用
iOS AFNetworking的使用
AFNetworking几乎是iOS上最常用的HTTP库了,AFNetworking也确实用起来简单,重要的API就那么几个,但是就那么几个就足够用了。下面介绍几个AFNetworking 2.x重要的API。
如果你要支持iOS6那么你应该要使用基于NSURLConnection封装的AFHTTPRequestOperationManager(推荐用manager,尽管用AFHTTPRequestOperation也可以
)。
GET请求
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];//manager.responseSerializer = [AFHTTPResponseSerializer serializer];//AFHTTPRequestOperationManager默认是json响应,如果你的服务端不是json响应则应该更改响应类型,用通用的http响应是个不错的选择NSDictionary *parameters = @{@"q1": @"query1", @"q2": @"query2"};//向服务器请求的参数,也就是查询条件[manager GET:@"http://www.yourcompany.com/service/afnetwork.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { //在这个地方解析服务器的响应,也就是responseObject } failure:^(AFHTTPRequestOperation *operation, NSError *error) { //处理错误 NSLog(@"Error: %@", error);}];
POST请求
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];NSDictionary *parameters = @{@"q1": @"query1", @"q2": @"query2"};[manager POST:@"http://www.yourcompany.com/service/afnetwork.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { //解析服务器的响应responseObject,是否要更改响应类型说明同上} failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error);}];
如果你要上传NSData数据或文件(比如图片)的话,可以使用如下POST API
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];NSDictionary *parameters = @{@"q1": @"query1", @"q2": @"query2"};NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];[manager POST:@"http://www.yourcompany.com/service/afnetwork.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileURL:filePath name:@"image"]; //[formData appendPartWitFileData:imgData name:@"image" fileName:@"filename" mimeType:@"image/jpeg"]} success:^(AFHTTPRequestOperation *operation, id responseObject) { //处理服务器端的响应responseObject,响应类型是否需要更改说明同上} failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error);}];
如果只支持iOS7及以上可以使用基于NSURLSession的API,可以看看官方指南URL Loading System
创建一个下载任务
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];//如果希望在后台继续下载,需配置后台sessionAFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];NSURL *URL = [NSURL URLWithString:@"http://www.yourcompany.com/service/download.zip"];NSURLRequest *request = [NSURLRequest requestWithURL:URL];NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { NSLog(@"File downloaded to: %@", filePath);}];[downloadTask resume];//data task要调用resume
创建一个上传任务
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];NSURL *URL = [NSURL URLWithString:@"http://www.yourcompany.com/service/upload.php"];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];
创建一个http/json/xml响应任务
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];NSURL *URL = [NSURL URLWithString:@"http://www.yourcompany.com/service/afnetwork.php"];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);//responseObject即为响应的json/xml数据 }}];[dataTask resume];
当然可以用更简单的办法,AFHTTPSessionManager提供了和AFHTTPRequestOperationManager类似的API
– GET:parameters:success:failure:– POST:parameters:success:failure:– POST:parameters:constructingBodyWithBlock:success:failure:
其调用方式和AFHTTPRequestOperationManager一样。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。