分类: IOS2013-12-15 20:29 12489人阅读 评论(5) 收藏 举报

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

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

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

GET 请求:

  1. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  2. [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
  3. NSLog(@"JSON: %@", responseObject);
  4. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  5. NSLog(@"Error: %@", error);
  6. }];

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

  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 Multi-Part格式的表单文件上传请求:

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

二、Session管理:
1、AFURLSessionManager:创建和管理制定的NSURLSession对象
2、NSURLSessionConfiguration对象必须实现<NSURLSessionTaskDelegate>, <NSURLSessionDataDelegate>, <NSURLSessionDownloadDelegate>, <NSURLSessionDelegate>协议

创建一个下载任务:

  1. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  2. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
  3. NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
  4. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  5. NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
  6. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  7. if (error) {
  8. NSLog(@"Error: %@", error);
  9. } else {
  10. NSLog(@"Success: %@ %@", response, responseObject);
  11. }
  12. }];
  13. [uploadTask resume];

创建一个数据流任务:

  1. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  2. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
  3. NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
  4. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  5. NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  6. if (error) {
  7. NSLog(@"Error: %@", error);
  8. } else {
  9. NSLog(@"%@ %@", response, responseObject);
  10. }
  11. }];
  12. [dataTask resume];

四、使用AFHTTPRequestOperation
1、AFHTTPRequestOperation是使用HTTP或HTTPS协议的AFURLConnectionOperation的子类。
它封装的获取后的HTTP状态和类型将决定请求的成功与否。
2、虽然AFHTTPRequestOperationManager通常是最好的去请求的方式,但是AFHTTPRequestOpersion也能够单独使用。

通过GET方式:

  1. NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.json"];
  2. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  3. AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  4. op.responseSerializer = [AFJSONResponseSerializer serializer];
  5. [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  6. NSLog(@"JSON: %@", responseObject);
  7. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  8. NSLog(@"Error: %@", error);
  9. }];
  10. [[NSOperationQueue mainQueue] addOperation:op];

连续操作多个:

  1. NSMutableArray *mutableOperations = [NSMutableArray array];
  2. for (NSURL *fileURL in filesToUpload) {
  3. NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  4. [formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];
  5. }];
  6. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  7. [mutableOperations addObject:operation];
  8. }
  9. NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:@[...] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
  10. NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);
  11. } completionBlock:^(NSArray *operations) {
  12. NSLog(@"All operations in batch complete");
  13. }];
  14. [[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];

AFNnetworking快速教程,官方入门教程译的更多相关文章

  1. TensorFlow 中文资源全集,官方网站,安装教程,入门教程,实战项目,学习路径。

    Awesome-TensorFlow-Chinese TensorFlow 中文资源全集,学习路径推荐: 官方网站,初步了解. 安装教程,安装之后跑起来. 入门教程,简单的模型学习和运行. 实战项目, ...

  2. TensorFlow 中文资源精选,官方网站,安装教程,入门教程,实战项目,学习路径。

    Awesome-TensorFlow-Chinese TensorFlow 中文资源全集,学习路径推荐: 官方网站,初步了解. 安装教程,安装之后跑起来. 入门教程,简单的模型学习和运行. 实战项目, ...

  3. OsharpNS轻量级.net core快速开发框架简明入门教程-Osharp.Redis使用

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  4. OsharpNS轻量级.net core快速开发框架简明入门教程-从零开始启动Osharp

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  5. OsharpNS轻量级.net core快速开发框架简明入门教程-代码生成器的使用

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  6. OsharpNS轻量级.net core快速开发框架简明入门教程-基于Osharp实现自己的业务功能

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  7. OsharpNS轻量级.net core快速开发框架简明入门教程-Osharp.Hangfire使用

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  8. OsharpNS轻量级.net core快速开发框架简明入门教程-Osharp.Permissions使用

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  9. OsharpNS轻量级.net core快速开发框架简明入门教程-切换数据库(从SqlServer改为MySql)

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

随机推荐

  1. mkbundle(1) - Linux man page

    mkbundle(1) - Linux man page Name mkbundle, mkbundle2 - Creates a bundled executable. Synopsis mkbun ...

  2. 《SDN核心技术剖析和实战指南》第一章小结

    第一章主要是概况.新技术有一个特点是,每家都有不同的说法.这里我只说说我比较认同的部分. SDN的核心概念大概有两个:转发面与控制面分离.开发可编程化.书里还说逻辑上集中控制,其实这个就可以从转发与控 ...

  3. Java学习笔记(1)——基本数据类型

    一.进制转换 10^n被称为权  10称为基数   计算机中正数和负数的关系是取反加一, 如: ~3+1=-3 补码边界运算有溢出风险 32位二进制补码最多表示2^32个数, -2G~2G 1,计算机 ...

  4. linux 切换用户之后变成-bash-x.x$的解决方法

    我们平时在linux下切换用户后命令行为什么会变成-bash-3.2$呢,我们来分析一下,这就是跟linux的机制有关联了,因为在linux下每次通过useradd创建新的用户时,都会将所有的配置文件 ...

  5. iOS socket 实现tcp和服务器长链接的简单使用心得

    首先iOS端用了一个第三方的框架 GCDAsyncSocket 当然这个是CocoaAsyncSocket框架里面的一部分 Github下载地址https://github.com/robbiehan ...

  6. mongodb的应用场景

    这篇文章总结的比较到位:http://www.tuicool.com/articles/YnmaAj

  7. Python Cookbook笔记

    字符串:s.strip()  删除字符串开始和结尾的空白字符. s.lstrip() 删除左边的,s.rstrip()  删除右边的. 随机数:random.random()  生成0-1之间的数. ...

  8. C#使用jmail组件发送邮件

    1.安装 命令行环境下,到jmail.dll所在目录,运行regsvr32 jmail.dll 2.代码 #region 发送邮件    /// <summary>    /// 发送邮件 ...

  9. 现代的新语言--Swift初探

    新的语言 WWDC简短的介绍,新的语言Swift就问世了,尽管新语言的名字导致贴吧下歌手粉丝和开发人员们争抢地盘- -,只是雨燕就是这么来了. WWDC keynote里给Swift打上了非常多标签: ...

  10. Qt 之容器内的控件全屏

    m_label = new QLabel(); ui->stackedWidget->addWidget(m_label); ui->stackedWidget->setCur ...