ASIHTTPRequest学习笔记
1、creating requests
request分为同步和异步两种。不同之处在于开始request的函数:
[request startSynchronous];
[request startAsynchronous];
其中,异步的request构造方式如下:
- (void) grabURLs
{
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];//url需要添加http://
ASIHTTPRequest *request;
//for (int i = 0; i < 5; i++) {
request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
// if (_queue == nil) {
// _queue = [[NSOperationQueue alloc] init];
// }else
// [_queue cancelAllOperations];
// request.userInfo = [NSString stringWithFormat:@"the index is %d",i];
//request.tag = i;
//[_queue addOperation:request];
[request startAsynchronous];
}
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
//NSLog(@"the content is %@.",responseString);
if (request.tag == 4) {
NSLog(@"lskdffj.");
}
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"the error is %@.",error);
}
在异步的request中,还可以通过下面的代码实现采用自己的函数执行成功和失败的操作:
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
如上面例子所示:我还可以创建一个NSOperationQueue,用于控制多个request的进程。当然可以采用更专业的ASINetworkQueues。
注:针对多个request调用相同的函数进行成功和失败的处理函数,但是还需要对各个request区别对待的场合,可以通过设置request的tag值或userInfo来区分。
2、ASINetworkQueues
(1)使用ASINetworkQueues和NSOperationQueue的不同之处在于,调用[queue go];才会真正的运行。
(2)同一个queue中的request,若有一个fail的话,后面的requests都会被cancel,修改方法为:
[queue setShouldCancelAllRequestsOnFailure:NO];
3、cancel 异步requests
(1)注:仅有异步的request才能被取消,同步的是取消不了的。
[request cancel];
或者[request clearDelegatesAndCancel];
对一个ASINetworkQueues对象可以通过[queue cancelAllOperations];实现。
(2)由于setDelegate函数,对与delegate并没有retain的操作,所以需要注意在delegate释放的时候,记得将request和queue的delegate设置为nil,并且取消对应的requests。
4、ASIFormDataRequest
- (void) loginAction
{
NSString *userName = self.username.text;
NSString *passWord = self.password.text;
NSLog(@"username:%@, password:%@",userName,passWord);
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8000/books/login/"];
//NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:userName forKey:@"username"];//setPostValue:forKey:
[request addPostValue:passWord forKey:@"password"];//setPostValue:forKey:
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(@"the data received is %@",response);
}else {
NSLog(@"error accured. %@",error);
}
}
同时还可以通过ASIFormDataRequest来实现文件上传,它可以自动的读取磁盘上的文件然后上传。
[request setFile:@"/Users/ben/Desktop/1.jpg" forKey:@"photo"];
5、下载数据
如果通过request获得的数据量非常大,可以通过函数将获得的数据直接存入磁盘上的一个文件内。
- (void) grabURLs
{
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
ASIHTTPRequest *request;
for (int i = 0; i < 5; i++) {
request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
if (_queue == nil) {
_queue = [[NSOperationQueue alloc] init];
}
request.userInfo = [NSString stringWithFormat:@"the index is %d",i];
request.tag = i;
NSString *filename = [NSString stringWithFormat:@"/Users/wukuniqiang/Desktop/%d.html",i];
[request setDownloadDestinationPath:filename];
[_queue addOperation:request];
//[request[i] startAsynchronous];
}
}
(本来想下载mp3的,但是校园网速度不给力,等了好久没反应,就变成个网页了)
下载进度的显示方法有两种:simple progress和 accurate progress
针对第一种,uiprogressview仅仅更新两次,0和100%
针对第二种,uiprogressview则随时更新。所以针对大文件的下载,推荐使用第二种方法。
request.showAccurateProgress = YES;
6、断点续传
断点续传需要做以下三步操作:
(1)设置下载的文件的位置
[request setDownloadDestinationPath:@"filename.txt.download"]
(3)设置采用断点续传
[request setAllowResumeForFileDownloads:YES];
7、基于流上传本地大文件
可以采用的方法有两种:
(1)ASIFormDataRequest的函数[request setFile: forKey:];
(2)ASIHTTPRequst的函数[request appendPostDataFromFile:@"/*/filename"];
ASIHTTPRequest中调用上诉函数之前,首先应该设置[request setShouldStreamPostDataFromDisk:YES];
注:上面两种方法不可混用
8、use a download cache
下面三种情况的时候需要采用download cache
1、当前没有网络连接并且不能再次下载
2、再次请求的数据和上次获得的数据有变化的时候
3、请求的数据永远不会变化,仅仅需要下载一次,以后可以放心使用的数据
ASIDownloadCache 设置下载缓存
它对Get请求的响应数据进行缓存(被缓存的数据必需是成功的200请求):
采用ASIHTTPRequest的类方法,对所有的request设置downloadcache:
| [ASIHTTPRequest setDownloadCache:[ASIDownloadCache sharedCache]]; |
当设置缓存策略后,所有的请求都被自动的缓存起来。
另外,如果仅仅希望某次请求使用缓存操作,也可以这样使用:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
多种的缓存并存
仅仅需要创建不同的ASIDownloadCache,并设置缓存所使用的路径,并设置到需要使用的request实例中:
ASIDownloadCache *cache = [[[ASIDownloadCache alloc] init] autorelease];[cache setStoragePath:@"/Users/ben/Documents/Cached-Downloads"];[self setMyCache:cache];ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];[request setDownloadCache:[self myCache]]; |
ASIHTTPRequest学习笔记的更多相关文章
- IOS学习笔记25—HTTP操作之ASIHTTPRequest
IOS学习笔记25—HTTP操作之ASIHTTPRequest 分类: iOS2012-08-12 10:04 7734人阅读 评论(3) 收藏 举报 iosios5网络wrapper框架新浪微博 A ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
- JAVA GUI编程学习笔记目录
2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...
- seaJs学习笔记2 – seaJs组建库的使用
原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...
- CSS学习笔记
CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...
- HTML学习笔记
HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...
随机推荐
- (转载)mysql:“Access denied for user 'root'@'localhost'”
原文地址:http://www.linuxidc.com/Linux/2007-05/4338.htm # /etc/init.d/mysql stop# mysqld_safe --user=mys ...
- Network Principle Course Summary 001
1.物理层 物理层 协议:RJ45.CLOCK.IEEE802.3 (中继器,集线器) 作用:通过媒介传输比特,确定机械及电气规范(比特Bit) 1.1 通信基础 数据 (data) —— 运送消息的 ...
- node 使用笔记
1 安装 buffertools 因为使用mjpeg-proxy的关系,需要编译buffertools中的C++代码文件,怎奈何一直出错. make: Entering directory `/srv ...
- word2vec 中的数学原理二 预备知识 霍夫曼树
主要参考: word2vec 中的数学原理详解 自己动手写 word2vec 编码的话,根是不记录在编码中的 这一篇主要讲的就是霍夫曼树(最优二叉树)和编码. ...
- hadoop2.6.4的HA集群搭建超详细步骤
hadoop2.0已经发布了稳定版本了,增加了很多特性,比如HDFS HA.YARN等.最新的hadoop-2.6.4又增加了YARN HA 注意:apache提供的hadoop-2.6.4的安装包是 ...
- CentOS 7.4 系统安装 git
CentOS 7.4 系统安装 git 一.使用 yum 安装 1.查看系统是否已经安装 git [root@localhost ~]# git --version 2.yum 安装 git [roo ...
- 【IObit】五大软件激活码( Advanced Systemcare....)
IObit Malware Fighter 6Pro 破解: 打开软件安装位置,下载替换dll文件 链接: https://pan.baidu.com/s/1Euz87MCANuCnRqZsMQ_w4 ...
- 洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]
题目传送门 晕牛Dizzy Cows 题目背景 Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. 题目描述 The cows have taken to racing each o ...
- maven自定义脚手架(快速生成项目)
Maven之自定义archetype生成项目骨架 利用脚手架生成 新项目 命令行方式 mvn archetype:generate \ -DarchetypeGroupId=com.xxx \ -Da ...
- IEnumerable<T>
IEnumerable 饮水思源 <C#本质论> Overview 根据定义,.Net 的中集合,本质上是一个类,它最起码实现了IEnumeraable 或者非泛型的IEnumerable ...