AFNnetworking快速教程,官方入门教程译

分类: IOS2013-12-15 20:29 12489人阅读 评论(5) 收藏 举报
afnetworkingjsonios入门教程快速教程

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

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

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

GET 请求:

[objc] view plaincopyprint?
  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请求:

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

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

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

创建一个下载任务:

[objc] view plaincopyprint?
  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];

创建一个数据流任务:

[objc] view plaincopyprint?
  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方式:

[objc] view plaincopyprint?
  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];

连续操作多个:

[objc] view plaincopyprint?
  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 中文资源精选,官方网站,安装教程,入门教程,实战项目,学习路径。

    转载至:http://www.nanjixiong.com/thread-122211-1-1.html Awesome-TensorFlow-Chinese TensorFlow 中文资源全集,学习 ...

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

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

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

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

  4. Unity游戏开发官方入门教程:飞机大战(六)——创建子弹

    Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...

  5. 第三章 Python Kivy 学习 -- Kivy官方入门教程Pong Game

    系列文章目录 第一章 Python Kivy 学习 – Kivy介绍及环境安装 第二章 Python Kivy 学习 – Kivy项目开发原理(待编辑) 第三章 Python Kivy 学习 – Ki ...

  6. Unity游戏开发官方入门教程:飞机大战(二)——创建飞船对象

    Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...

  7. 用TypeScript来写React官方入门教程 .tsx后缀文件,同时入门typescript和React

    用TypeScript来写React官方入门教程 .tsx后缀文件,同时入门typescript和React 1. 项目说明: 这是React官网上那个下井字棋的入门教程,但是我把它换了typesci ...

  8. Unity游戏开发官方入门教程:飞机大战(五)——实现飞船控制脚本

    Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...

  9. snap7使用说明中文版_Python官方入门教程_中文版_3.7.3

    张小森:Python官方入门教程/2. 使用 Python 解释器​zhuanlan.zhihu.com 张小森:Python官方入门教程_中文版_3. Python 的非正式介绍​zhuanlan. ...

最新文章

  1. delphi 判断鼠标 左右_外设评测HyperX Pulsefire Haste游戏鼠标分享
  2. Idea不能新建package的解决
  3. JSP获得客服端MAC地址
  4. Linux echo 显示内容颜色
  5. Angular Component 实现类,先执行字段初始化,再调用构造函数
  6. 巧用FlashPaper 让Word文档变Flash
  7. java boolean 包_java Boolean包装类工作笔记
  8. Ionic3与Angular4新特性
  9. sas不能安装独立的java_SAS安装问题解决办法
  10. wpf-AvalonDock基础-安装和更换主题
  11. UEFI原理与编程(二):UEFI工程模块文件-标准应用程序工程模块
  12. 网管必用的10款软件系统
  13. 适合苹果4s的微信版本_6.1.3装上微信了,新手看这里
  14. java.lang.IllegalArgumentException: 字符[_]在域名中永远无效。 at
  15. python画气泡图_用python 来绘制气泡图的简单技巧
  16. JMF的安装与环境的配置
  17. 3维线程格 gpu_GPU的线程模型和内存模型
  18. 调试 Windows 中的调试
  19. UVA 11165 - Galactic Travel(BFS+twopointer+并查集)
  20. 夺命雷公狗---DEDECMS----20dedecms取出栏目页对应的内容

热门文章

  1. 新产品Wyn Enterprise 详解,立即预约公开课
  2. 轻松搞定对容器实例日志设置定期清理和回卷 1
  3. 1002. 三角形 (
  4. ViewPager 简单实现左右无限滑动.
  5. linux软件安装方法
  6. 【前端】数组元素过滤
  7. Java面试题系列(X)优化tomcat配置
  8. 封装一个操作文件的函数
  9. 自定义git忽略规则
  10. 【Uva 11604 编码都有歧义了】