AFNnetworking快速教程,官方入门教程译
AFNnetworking快速教程,官方入门教程译
AFNetworking官网入门教程简单翻译,学习
AFNetworking 是一个能够快速使用的ios和mac os x下的网络框架,它是构建在Foundation URL Loading System之上的,封装了网络的抽象层,可以方便的使用,AFNetworking是一个模块化架构,拥有丰富api的框架。
一、HTTP请求与操作:
1、AFHTTPRequestOperationManager:
该类封装与Web应用程序进行通信通过HTTP,包括要求制作,响应序列化,网络可达性监控和安全性,以及要求经营管理的常见模式。
GET 请求:
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
- [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"JSON: %@", responseObject);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"Error: %@", error);
- }];
POST 带有表单参数的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 Multi-Part格式的表单文件上传请求:
- 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);
- }];
二、Session管理:
1、AFURLSessionManager:创建和管理制定的NSURLSession对象
2、NSURLSessionConfiguration对象必须实现<NSURLSessionTaskDelegate>, <NSURLSessionDataDelegate>, <NSURLSessionDownloadDelegate>, <NSURLSessionDelegate>协议
创建一个下载任务:
- 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];
创建一个数据流任务:
- NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
- AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
- NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
- 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);
- }
- }];
- [dataTask resume];
四、使用AFHTTPRequestOperation
1、AFHTTPRequestOperation是使用HTTP或HTTPS协议的AFURLConnectionOperation的子类。
它封装的获取后的HTTP状态和类型将决定请求的成功与否。
2、虽然AFHTTPRequestOperationManager通常是最好的去请求的方式,但是AFHTTPRequestOpersion也能够单独使用。
通过GET方式:
- NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.json"];
- NSURLRequest *request = [NSURLRequest requestWithURL:URL];
- AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
- op.responseSerializer = [AFJSONResponseSerializer serializer];
- [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"JSON: %@", responseObject);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"Error: %@", error);
- }];
- [[NSOperationQueue mainQueue] addOperation:op];
连续操作多个:
- NSMutableArray *mutableOperations = [NSMutableArray array];
- for (NSURL *fileURL in filesToUpload) {
- NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
- [formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];
- }];
- AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
- [mutableOperations addObject:operation];
- }
- NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:@[...] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
- NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);
- } completionBlock:^(NSArray *operations) {
- NSLog(@"All operations in batch complete");
- }];
- [[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];
AFNnetworking快速教程,官方入门教程译相关推荐
- TensorFlow 中文资源精选,官方网站,安装教程,入门教程,实战项目,学习路径。
转载至:http://www.nanjixiong.com/thread-122211-1-1.html Awesome-TensorFlow-Chinese TensorFlow 中文资源全集,学习 ...
- OsharpNS轻量级.net core快速开发框架简明入门教程-代码生成器的使用
OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...
- OsharpNS轻量级.net core快速开发框架简明入门教程-基于Osharp实现自己的业务功能...
OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...
- Unity游戏开发官方入门教程:飞机大战(六)——创建子弹
Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...
- 第三章 Python Kivy 学习 -- Kivy官方入门教程Pong Game
系列文章目录 第一章 Python Kivy 学习 – Kivy介绍及环境安装 第二章 Python Kivy 学习 – Kivy项目开发原理(待编辑) 第三章 Python Kivy 学习 – Ki ...
- Unity游戏开发官方入门教程:飞机大战(二)——创建飞船对象
Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...
- 用TypeScript来写React官方入门教程 .tsx后缀文件,同时入门typescript和React
用TypeScript来写React官方入门教程 .tsx后缀文件,同时入门typescript和React 1. 项目说明: 这是React官网上那个下井字棋的入门教程,但是我把它换了typesci ...
- Unity游戏开发官方入门教程:飞机大战(五)——实现飞船控制脚本
Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...
- snap7使用说明中文版_Python官方入门教程_中文版_3.7.3
张小森:Python官方入门教程/2. 使用 Python 解释器zhuanlan.zhihu.com 张小森:Python官方入门教程_中文版_3. Python 的非正式介绍zhuanlan. ...
最新文章
- delphi 判断鼠标 左右_外设评测HyperX Pulsefire Haste游戏鼠标分享
- Idea不能新建package的解决
- JSP获得客服端MAC地址
- Linux echo 显示内容颜色
- Angular Component 实现类,先执行字段初始化,再调用构造函数
- 巧用FlashPaper 让Word文档变Flash
- java boolean 包_java Boolean包装类工作笔记
- Ionic3与Angular4新特性
- sas不能安装独立的java_SAS安装问题解决办法
- wpf-AvalonDock基础-安装和更换主题
- UEFI原理与编程(二):UEFI工程模块文件-标准应用程序工程模块
- 网管必用的10款软件系统
- 适合苹果4s的微信版本_6.1.3装上微信了,新手看这里
- java.lang.IllegalArgumentException: 字符[_]在域名中永远无效。 		at
- python画气泡图_用python 来绘制气泡图的简单技巧
- JMF的安装与环境的配置
- 3维线程格 gpu_GPU的线程模型和内存模型
- 调试 Windows 中的调试
- UVA 11165 - Galactic Travel(BFS+twopointer+并查集)
- 夺命雷公狗---DEDECMS----20dedecms取出栏目页对应的内容