使用 ASIHttpRequest 的注意事项
来源:互联网 发布:手机数据删除怎么恢复 编辑:程序博客网 时间:2024/04/29 13:35
1. 如何把ASIHTTPRequest添加到自己的ios工程里
关于这一点官方的文档已经有详细的说明,我把它列出来只是为了方便日后参考。
首先需要把源码里的文件引用到现有工程,如果只是用到基础功能的话,只需引用下图所示的文件:
然后需要添加CFNetwork, SystemConfiguration, MobileCoreServices, CoreGraphics 和 zlib这些framework,添加方法如下图:
详细的配置文档请参考:http://allseeing-i.com/ASIHTTPRequest/Setup-instructions
2. 使用ASINetworkQueue下载出错
我的下载队列大概有十几个图片,在我确保网络连接没有问题的情况下,每次[queue go]之后总有一两张图片下载失败,这个问题困扰了我很久,不知道问题出在哪里。我的queue的配置如下:
[mNetworkQueue reset];
[mNetworkQueue setDownloadProgressDelegate:progressIndicator];
[progressIndicator release];
[mNetworkQueue setRequestDidFinishSelector:@selector(converFetchComplete:)];
[mNetworkQueue setRequestDidFailSelector:@selector(converFetchFailed:)];
[mNetworkQueue setShowAccurateProgress:YES];
[mNetworkQueue setDelegate:self];
[mNetworkQueue setQueueDidFinishSelector:@selector(converQueueComplete:)];
之后经过反复的调试,我发现是setShowAccurateProgress这个方法引起的,如果不配置这个方法,下载过程就不会出错。但是我的用法是参考了ASIHTTPRequest官方的sample的用法,应该没有什么区别(参考:QueueViewController.m)。虽然问题解决了,但是导致下载失败的根本原因没有搞清楚,回头再看看源码,找到问题的根源再来更新。
3. setDownloadDestinationPath 不会自动创建路径
之前用[SSZipArchive unzipFileAtPath:targetPath toDestination:destinationPath]的时候如果目标路径不存在会自动创建,想当然的以为ASIHTTPRequest的setDownloadDestinationPath方法也回自动创建,事实证明这种想法是错误的,如果目标路径不存在会导致request失败。解决方法是在 设置之前先判断目标路径是否存在,不存在就手动创建好,参见如下代码:
if (![[NSFileManager defaultManager] fileExistsAtPath:[targetPath stringByDeletingLastPathComponent]])
{
[[NSFileManager defaultManager]
createDirectoryAtPath:[targetPath stringByDeletingLastPathComponent]
withIntermediateDirectories:YES attributes:nil error:nil];
}
4. 用ASIHTTPRequest下载文件,如果请求的url不存在,不会报告错误
用ASIHTTPRequest下载文件时,如果请求的文件不存在,并不会触发requestFailed,它会下载一个描述错误的文本文件保存到目标路径,用文本编辑器打开后如下所示:
这个文件描述的是404错误,这种情况下怎么判断我下载的文件是否是我真正要下载的呢,解决方法是在setRequestDidFinishSelector指定的selector中加入404状态的判断,代码如下:
- (void)converFetchComplete:(ASIHTTPRequest *)request
{
NSLog(@"request name is %@", [request.userInfo objectForKey:@"name"]);
if( [request responseStatusCode] == 404 )// 判断是否发生404错误
{
NSLog(@"the requested url was not found!!");
if ([[NSFileManager defaultManager] fileExistsAtPath:request.downloadDestinationPath]) {
[[NSFileManager defaultManager] removeItemAtPath:request.downloadDestinationPath error:NULL];
}
}
}
注:其中converFetchComplete为我指定的setRequestDidFinishSelector。
5. 需要注意的有关内存回收的问题
关于这一点,ASIHTTPRequest的官方文档中有提到一些:Requests don’t retain their delegates, so if there’s a chance your delegate may be deallocated while your request is running, it is vital that you clear the request’s delegate properties. In most circumstances, if your delegate is going to be deallocated, you probably also want to cancel request, since you no longer care about the request’s status.
官方给出的代码示例如下:
// Ddealloc method for our controller
- (void)dealloc
{
[request clearDelegatesAndCancel];
[request release];
...
[super dealloc];
}
这里只说明了request回收的问题,如果是ASINetworkQueue的话也存在同样的内存回收的问题,解决方法如下:
// Ddealloc method for our controller
- (void)dealloc
{
// request 回收
[mRequest clearDelegatesAndCancel];
[mRequest release];
// networkQueue 回收
[mNetworkQueue reset];
[mNetworkQueue release];
...
[super dealloc];
}
- 使用 ASIHttpRequest 的注意事项
- 使用ASIhttpRequest上传图片 注意事项
- ASIHttpRequest中Block代码块的使用和注意事项
- ASIHTTPRequest-Cookie的使用
- ASIHTTPRequest-Cookie的使用
- ASIHTTPRequest-Cookie的使用
- ASIHTTPRequest的使用
- ASIHttpRequest的使用
- ASIHTTPRequest的使用
- ASIHTTPRequest-插件的使用
- ASIHTTPRequest-Cookie的使用
- ASIHTTPRequest-Cookie的使用
- ASIHttprequest的使用
- ASIHTTPRequest-Cookie的使用
- ASIHttprequest的使用
- ASIHttpRequest的使用
- ASIHTTPRequest-Cookie的使用
- ASIHTTPRequest的使用
- 概率论07 联合分布
- Android手机短信,通话记录和通讯录的相关操作
- HTML5不能完成的5件事
- oracle中的empdp和impdp简单记录
- Linux下top命令后参数含义
- 使用 ASIHttpRequest 的注意事项
- android中两种方式打开网页
- Windows 8.1及Windows8上的java JDK环境变量配置---以爱丶为名
- Java中的==和equals区别
- 字符串的连接与求子串
- Oracle数据恢复顾问(DRA)使用测试
- 微软官方Windows7 Ultimate with SP1 DVD 官方简体中文原版下载
- QTableWidget 应用总结
- Spring集成Quartz定时任务框架介绍和Cron表达式详解