IOS网络--AFNetworking

http://cocoadocs.org/docsets/AFNetworking/2.5.0/html

AFNetworking的用法ios

  1. 提交GET请求和POST请求web

AFNetworking是第三方框架,阅者自行去官网上下载、安装。json

1>建立AFHTTPRequestOpeartionManger对象api

2>根据服务器内容不一样,为AFHTTPRequestOpeartionManger对象指定不一样的解析器,该对象默认的解析器是JSON和plist文件解析器。服务器

3>发送GET请求,用Manager对象调用GET方法便可,success代码块和failure代码块是在网络请求成功/失败后调用。网络

4>success参数指定了代码块中处理服务器响应成功的正确数据;failure参数指定了代码块中处理服务器响应失败的错误数据。app

AFHTTPRquestOperationManager
框架

包含了常见的HTTP访问web站点的模式,有建立请求、链接响应、网络类型监视等。url

“GET”请求格式:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//调用get方法
[manager GET:@“http://example.com/resources.json”  parameters:parameters 
//加载成功的代码块
success:^(AFHTTPRequestOperation *operation,id responseObject)
         {
             NSLog(@"JSON:%@",responseObject);
         }
//加载失败的代码块
failure:^(AFHTTPRequestOperation *operation,NSError *error)
         {
             NSLog(@"Error:%@",error);
         }];

"POST"请求格式:

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"多个请求

//建立对象
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);
}];

2.建立一个下载文件任务

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/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];

3.建立一个跟新(上传)文件任务

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];

4.处理JSON或plist响应

IOS应用在处理JSON和Plist响应的时候能够将其转换成NSDictionary对象或是NSArray对象,AFHTTPRequestOperationManager默承认以处理JSON或plist响应,所以当响应内容默认为JSON时,无需再次指定服务器响应解析器。

NSDictionary *temDic = [[NSDictionary alloc] init];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSString *url = @"http://api.map.baidu.com/telematics/v3/weather?";
        NSDictionary *parameters = @{@"location":@"长沙",@"output":@"json",@"ok":@"akljhgffg"};
        [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation,id responseObject)
         {
             NSLog(@"JSON:%@",responseObject);
             temDic = responseObject;
             
             
         }failure:^(AFHTTPRequestOperation *operation,NSError *error)
         {
             NSLog(@"Error:%@",error);
         }];



http://www.cocoachina.com/ios/20141120/10265.html

相关文章
相关标签/搜索