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];//如果希望在后台继续下载,需配置后台session AFURLSessionManager *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一样。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。