ASIHTTPRequest简单使用
来源:互联网 发布:泛雅网络教学平台下载 编辑:程序博客网 时间:2024/06/05 06:24
ASIHTTPRequest组件下载地址https://github.com/pokeb/asi-http-request/
ASIHTTPRequest对CFNetwork进行了很好的封装,提供了一个比官方更方便更强大的HTTP网络传输的封装。它的主要特色功能如下:
- 下载的数据,可存储到内存中或直接存储到磁盘中
- 能上传本地文件到服务端
- 可以方便的访问和操作请求和返回的Http头信息
- 可以获取到上传或下载的进度信息,为应用程序提供更好的体验
- 支持上传或下载队列,并且可获取队列的进度信息
- 支持Cookie;以及加压GZIP文件
- 内置的ASIDownloadCache类,可以缓存请求返回的数据,这样即使没有网络也可以返回已经缓存的数据结果(新闻类最实用)
- 支持同步和异步请求
- 支持大文件断点续传
1)、首先到上面的地址去下载最新的包;下载完之后add(copy)到项目里。
2)、添加需要的框架:CFNetwork.framework、SystemConfiguration.framework、 MobileCoreServices.framework、CoreGraphics.framework和libz.1.2.*.dylib
使用方法:
1)、在文件头部导入包 #import "ASIHTTPRequest.h"
同步请求(实际项目中不适用)
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]]; [request startSynchronous]; NSError *error = [request error]; if (!error) { NSString *dataStr = [request responseString]; //处理数据 }
异步请求:要实现ASIHTTPRequestDelegate接口
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]]; request.delegate = self; [request startAsynchronous];- (void)requestFinished:(ASIHTTPRequest *)request { NSData *data = [request responseData]; NSString *dataStr = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; NSMutableDictionary *dataDic = [dataStr JSONValue]; NSLog(@"data : %@",dataDic);}- (void)requestFailed:(ASIHTTPRequest *)request { NSError *error = [request error]; NSLog(@"error : %@",error);}队列请求
[_networkQueue setMaxConcurrentOperationCount:2]; _networkQueue = [[NSOperationQueue alloc] init]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@""]]; [request setDidFinishSelector:@selector(doRequestFinish:)]; [request setDidFailSelector:@selector(doRequestFailed:)]; [_networkQueue addOperation:request];- (void)doRequestFinish:(ASIHTTPRequest *)request { //处理}- (void)doRequestFailed:(ASIHTTPRequest *)request { //处理}如果使用ASINetworkQueues(必须调用[_networkQueue go]才能启动),可以跟踪;可以设置回调的方法:SEL requestDidStartSelector;SEL requestDidReceiveResponseHeadersSelector;SEL requestWillRedirectSelector;SEL requestDidFinishSelector;SEL queueDidFinishSelector;
带参数的Post请求(ASIFormDataRequest 继承 ASIHTTPRequest)
ASIFormDataRequest *formRequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@""]]; [formRequest addPostValue:@"value" forKey:@"key"]; [formRequest startAsynchronous];
同步、异步取消请求[request cancel]; //取消后还会调用delegate方法
[request clearDelegatesAndCancel] //取消后不会调用delegate方法
队列取消请求[_networkQueue setShouldCancelAllRequestsOnFailure:NO]; //只对某一特定的取消
[_networkQueue cancelAllOperations]; //明确取消全部
下载[request setDownloadDestinationPath:path]; //设置下载地址 [request setTemporaryFileDownloadPath:path]; //设置下载时的临时文件地址
如果想跟踪下载、上传的进度,可以实现ASIProgressDelegate接口[request setDownloadProgressDelegate:self]; //下载
[request setuploadProgressDelegate:self]; //上传
- (void)setProgress:(float)newProgress; //实现该方法
缓存ASIDownloadCache *downloadCache = [[ASIDownloadCache alloc] init]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cacheDirectory = [paths objectAtIndex:0]; //获得缓存Cache文件的地址 [downloadCache setStoragePath:[cacheDirectory stringByAppendingString:@"imgSource"]]; [request setDownloadCache:downloadCache]; [request setCachePolicy:ASIDontLoadCachePolicy]; //缓存的策略 [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; //缓存存储方式
ASIUseDefaultCachePolicy 这是一个默认的缓存策略“ASIAskServerIfModifiedWhenStaleCachePolicy”,这个很明白,见名知意(它不能与其它策略组合使用)ASIDoNotReadFromCacheCachePolicy 所读数据不使用缓存ASIDoNotWriteToCacheCachePolicy 不对缓存数据进行写操作ASIAskServerIfModifiedWhenStaleCachePolicy 默认缓存行为,request会先判断是否存在缓存数据。a, 如果没有再进行网络请求。 b,如果存在缓存数据,并且数据没有过期,则使用缓存。c,如果存在缓存数据,但已经过期,request会先进行网络请求,判断服务器版本与本地版本是否一样,如果一样,则使用缓存。如果服务器有新版本,会进行网络请求,并更新本地缓存ASIAskServerIfModifiedCachePolicy 与默认缓存大致一样,区别仅是每次请求都会 去服务器判断是否有更新ASIOnlyLoadIfNotCachedCachePolicy 如果有缓存在本地,不管其过期与否,总会拿来使用ASIDontLoadCachePolicy 仅当有缓存的时候才会被正确执行,如果没有缓存,request将被取消(没有错误信息)ASIFallbackToCacheIfLoadFailsCachePolicy 这个选项经常被用来与其它选项组合使用。请求失败时,如果有缓存当网络则返回本地缓存信息(这个在处理异常时非常有用)ASICacheForSessionDurationCacheStoragePolicy 默认的存储方式;基于Session的缓存数据存储;注意:[ASIHTTPRequest clearSession]将会使其失效ASICachePermanentlyCacheStoragePolicy 缓存永久保存在本地
ASIHTTPRequest的其他特性:设置缓存时间:/秒
[request setSecondsToCache:60];
退到后台是否继续网络请求:
[request setShouldContinueWhenAppEntersBackground:YES];
判断当前是否在网络请求:[ASIHTTPRequest isNetworkInUse];设置重连的次数
[request setNumberOfTimesToRetryOnTimeout:2];
这里只是本人经常用的几点;详细的可见官方API.
- ASIHTTPRequest 之简单使用
- ASIHTTPRequest简单使用
- ASIHTTPRequest 简单使用
- ASIHTTPRequest框架的简单使用
- 网络之ASIHttpRequest和SBJson简单使用 .
- ASIHTTPRequest类简介和简单使用
- ASIHTTPRequest使用
- ASIHTTPRequest 使用
- ASIHTTPRequest使用
- ASIHTTPRequest使用
- ASIHTTPRequest使用
- ASIHttpRequest 使用
- ASIHTTPRequest使用
- IOS学习十四:网络之ASIHttpRequest和SBJson简单使用
- IOS学习十四:网络之ASIHttpRequest和SBJson简单使用
- asihttprequest简单异步
- ASIHttpRequest简单用法
- ASIHTTPRequest源码简单分析
- Ubuntu12.04 64位安装Foxit Reader
- Navicat 数据模型
- 敏捷开发一千零一问系列之三十五:如何获取准确需求?(兼谈精益创业)
- android关于scrollview嵌套ExpandableListView的实现2
- 浅谈android应用启动数据迁移
- ASIHTTPRequest简单使用
- Cannot connect to xxx.xxx.xxx.xxx when downloading iOS app by OTA
- Android 编译 zlib
- DWR + ssh2 整合
- 关于jqgrid中如何从editurl取得返回值的解决方法
- linux下mysql root密码丢失
- OWC11绘制双轴图表
- VC++6.0在64位Win7下不能安装、VC++6与Office2007(10)不兼容的问题
- 出现 error:unknown filesystem grub rescue解决办法