afnetworking导入哪些框架
展开全部
AFNetworking的用法
1、提交GET请求和提交POST请求
AFNetworking是第三方的框架,所以需要开发者自行下载,安装。并在AFNetworking.h文件导入#import“AFHTTPRequestOpeartionManager.h ”,把AFNetworking.h头文件放入prefix文件中。 a、创建AFHTTPRequestOpeartionManger对象
b、根据服务器内容的不同,为AFHTTPRequestOpeartionManger对象指定不同的解析器,该对象默认的解析器是JSON和Plist文件解析器。如果服务器的数据是XML格式则需要手动的更改解析器
c、发送GET请求: 用Manager对象调用 GET:parameters:success:failure:方法即可,success代码块和failue代码块在网络请求成功/失败过后调用。
d、success:参数指定了代码块中处理服务器响应成功的正确数据,failue:参数指定了代码块中处理服务器响应失败的错误数据、
AFHTTPRquestOperationManager
包含了常见的HTTP访问web站点的模式,有创建请求,连续的响应,网络类型监视以及安全。 “GET”:
[objc] view plaincopyprint?
1. <span style="font-size:12px;">//创建AFHTTPRequestOperationManager对象 2. AFHTTPRequestOperationManager *manager =
[AFHTTPRequestOperationManger manager];
3. //调用get方法
4. [manager GET:@“http://example.com/resources.json”parameters : parameters 5. //加载成功的代码块,可以接收数据
6. success:^(AFHTTPRequestOperation *operation,id responseobject)]{
7. NSLog(@“json“:%@”,responseObject);
8. }failure:^(AFHTTPRequestOperation *operation,NSError *error){
9. NSLog(@“Error:%@”,error);
10. }];</span> ”POST“:URL-Form-Encoded Request URL编码请求类型
[objc] view plaincopyprint?
1. AFHTTPRequestOperationManager *manager =
[AFHTTPRequestOperationManager manager]; 2. NSDictionary *parameters = @{@"foo": @"bar"};
3. [manager POST:@"http://example.com/resources.json" parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
4. NSLog(@"JSON: %@", responseObject);
5. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
6. NSLog(@"Error: %@", error);
7. }];
"POST"多个请求
[objc] view plaincopyprint?
1. AFHTTPRequestOperationManager *manager =
[AFHTTPRequestOperationManager manager];
2. NSDictionary *parameters = @{@"foo": @"bar"};
3. NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
4. [manager POST:@"http://example.com/resources.json"
parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 5. [formData appendPartWithFileURL:filePath name:@"image" error:nil];
6. } success:^(AFHTTPRequestOperation *operation, id responseObject) { 7. NSLog(@"Success: %@", responseObject); 8. } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 9. NSLog(@"Error: %@", error);
10. }];
2、创建一个下载文件的任务
AFURLSessionManager创建并完善了一个NSURLSession的对象基于遵从NSURLSessionDelegate与NSURLSessionDataDelegate协议NSURLSessionConfigration对象。
[objc] view plaincopyprint?
1. NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
2. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
3.
URLWithString:@"http://example.com/download.zip"];
5. NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 6.
7. NSURLSessionDownloadTask *downloadTask = [manager
downloadTaskWithRequest:request progress:nil destination:^NSURL
*(NSURL *targetPath, NSURLResponse *response) {
8. NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
9. return [documentsDirectoryURL
URLByAppendingPathComponent:[response suggestedFilename]];
10. } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
11. NSLog(@"File downloaded to: %@", filePath);
12. }];
13. [downloadTask resume];
3、创建一个上传文件的任务
[objc] view plaincopyprint?
1. NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
2. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
3. 4. NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
5. NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 6. 7. NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
8. NSURLSessionUploadTask *uploadTask = [manager
uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject,
NSError *error) {
10. NSLog(@"Error: %@", error);
11. } else {
12. NSLog(@"Success: %@ %@", response, responseObject);
13. }
14. }];
15. [uploadTask resume];
4、处理JSON或Plist响应
IOS应用在处理JSON和Plist响应的时候可以十分轻便将其转换成NSDictionary对象或者NSArray对像,AFHTTPRequestOpeartionManager默认就可以处理JSON或Plist响应,也就是说当我们response.MIMEType为appication/json、
text/json,AFHTTPRequestOpeartionManager默认就可以处理,无需再次指定服务器响应解析器。
[objc] view plaincopyprint? 1.
2. AFHTTPRequestOperationManager *manager =
[AFHTTPRequestOperationManager manager];
3. // manager.responseSerializer = [AFHTTPResponseSerializer serializer];
4. NSDictionary *parameter = @{@"location":@"长沙
",@"output":@"json",@"ak":@"jlflVx1VTUahj05Q7GfB7PCf"};
5. [manager GET:@"http://api.map.uu456.com/telematics/v3/weather?"
parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
6. NSLog(@"OK");
7. dic = responseObject;
8. NSArray *keys = [dic allKeys];
9. NSLog(@"%@",keys);
10. // _datas = responseObject;
11. // NSString *stringData = [[NSString alloc]initWithData:_datas
encoding:NSUTF8StringEncoding];
12. // NSLog(@"%@",stringData);
1、提交GET请求和提交POST请求
AFNetworking是第三方的框架,所以需要开发者自行下载,安装。并在AFNetworking.h文件导入#import“AFHTTPRequestOpeartionManager.h ”,把AFNetworking.h头文件放入prefix文件中。 a、创建AFHTTPRequestOpeartionManger对象
b、根据服务器内容的不同,为AFHTTPRequestOpeartionManger对象指定不同的解析器,该对象默认的解析器是JSON和Plist文件解析器。如果服务器的数据是XML格式则需要手动的更改解析器
c、发送GET请求: 用Manager对象调用 GET:parameters:success:failure:方法即可,success代码块和failue代码块在网络请求成功/失败过后调用。
d、success:参数指定了代码块中处理服务器响应成功的正确数据,failue:参数指定了代码块中处理服务器响应失败的错误数据、
AFHTTPRquestOperationManager
包含了常见的HTTP访问web站点的模式,有创建请求,连续的响应,网络类型监视以及安全。 “GET”:
[objc] view plaincopyprint?
1. <span style="font-size:12px;">//创建AFHTTPRequestOperationManager对象 2. AFHTTPRequestOperationManager *manager =
[AFHTTPRequestOperationManger manager];
3. //调用get方法
4. [manager GET:@“http://example.com/resources.json”parameters : parameters 5. //加载成功的代码块,可以接收数据
6. success:^(AFHTTPRequestOperation *operation,id responseobject)]{
7. NSLog(@“json“:%@”,responseObject);
8. }failure:^(AFHTTPRequestOperation *operation,NSError *error){
9. NSLog(@“Error:%@”,error);
10. }];</span> ”POST“:URL-Form-Encoded Request URL编码请求类型
[objc] view plaincopyprint?
1. AFHTTPRequestOperationManager *manager =
[AFHTTPRequestOperationManager manager]; 2. NSDictionary *parameters = @{@"foo": @"bar"};
3. [manager POST:@"http://example.com/resources.json" parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
4. NSLog(@"JSON: %@", responseObject);
5. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
6. NSLog(@"Error: %@", error);
7. }];
"POST"多个请求
[objc] view plaincopyprint?
1. AFHTTPRequestOperationManager *manager =
[AFHTTPRequestOperationManager manager];
2. NSDictionary *parameters = @{@"foo": @"bar"};
3. NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
4. [manager POST:@"http://example.com/resources.json"
parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 5. [formData appendPartWithFileURL:filePath name:@"image" error:nil];
6. } success:^(AFHTTPRequestOperation *operation, id responseObject) { 7. NSLog(@"Success: %@", responseObject); 8. } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 9. NSLog(@"Error: %@", error);
10. }];
2、创建一个下载文件的任务
AFURLSessionManager创建并完善了一个NSURLSession的对象基于遵从NSURLSessionDelegate与NSURLSessionDataDelegate协议NSURLSessionConfigration对象。
[objc] view plaincopyprint?
1. NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
2. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
3.
URLWithString:@"http://example.com/download.zip"];
5. NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 6.
7. NSURLSessionDownloadTask *downloadTask = [manager
downloadTaskWithRequest:request progress:nil destination:^NSURL
*(NSURL *targetPath, NSURLResponse *response) {
8. NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
9. return [documentsDirectoryURL
URLByAppendingPathComponent:[response suggestedFilename]];
10. } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
11. NSLog(@"File downloaded to: %@", filePath);
12. }];
13. [downloadTask resume];
3、创建一个上传文件的任务
[objc] view plaincopyprint?
1. NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
2. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
3. 4. NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
5. NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 6. 7. NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
8. NSURLSessionUploadTask *uploadTask = [manager
uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject,
NSError *error) {
10. NSLog(@"Error: %@", error);
11. } else {
12. NSLog(@"Success: %@ %@", response, responseObject);
13. }
14. }];
15. [uploadTask resume];
4、处理JSON或Plist响应
IOS应用在处理JSON和Plist响应的时候可以十分轻便将其转换成NSDictionary对象或者NSArray对像,AFHTTPRequestOpeartionManager默认就可以处理JSON或Plist响应,也就是说当我们response.MIMEType为appication/json、
text/json,AFHTTPRequestOpeartionManager默认就可以处理,无需再次指定服务器响应解析器。
[objc] view plaincopyprint? 1.
2. AFHTTPRequestOperationManager *manager =
[AFHTTPRequestOperationManager manager];
3. // manager.responseSerializer = [AFHTTPResponseSerializer serializer];
4. NSDictionary *parameter = @{@"location":@"长沙
",@"output":@"json",@"ak":@"jlflVx1VTUahj05Q7GfB7PCf"};
5. [manager GET:@"http://api.map.uu456.com/telematics/v3/weather?"
parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
6. NSLog(@"OK");
7. dic = responseObject;
8. NSArray *keys = [dic allKeys];
9. NSLog(@"%@",keys);
10. // _datas = responseObject;
11. // NSString *stringData = [[NSString alloc]initWithData:_datas
encoding:NSUTF8StringEncoding];
12. // NSLog(@"%@",stringData);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询