网络编程(四)ASIHTTPRequest 框架的基本使用
- 导入类库:
- ASIHTTPRequest 依赖 Reachability 类,所以需要同时导入 ASIHTTPRequest 和 Reachability 2个类库:
- 设置只这里一个类库不使用 ARC,项目中其他部分仍然使用 ARC:
1 //创建请求对象 2 NSURL *url = [NSURL URLWithString:@"你的url"]; 3 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 4 request.timeOutSeconds = 10; 5 6 //发送同步请求 7 [request startSynchronous]; 8 9 //检测服务器返回的结果 10 if (request.error) { 11 12 // 请求出错(比如超时) 13 14 } else { 15 16 // 请求成功 17 }
1 //创建请求对象 2 NSURL *url = [NSURL URLWithString:@"你的url"]; 3 self.request = [ASIHTTPRequest requestWithURL:url]; 4 self.request.timeOutSeconds = 10; 5 6 //设置 request 的代理 7 self.request.delegate = self; 8 9 //发送请求 10 [self.request startAsynchronous]; 11 12 #pragma mark 代理方法 13 - (void)requestFinished:(ASIHTTPRequest *)request 14 { 15 //request.responseData : 服务器返回的全部数据 16 } 17 18 - (void)requestFailed:(ASIHTTPRequest *)request 19 { 20 21 } 22 23 - (void)requestStarted:(ASIHTTPRequest *)request 24 { 25 26 } 27 28 - (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data 29 { 30 31 }
1 - (void)dealloc 2 { 3 //必须在控制器销毁的时候, 取消请求代理 4 [self.request clearDelegatesAndCancel]; 5 } 6
1 //创建请求对象 2 NSURL *url = [NSURL URLWithString:@"你的url"]; 3 self.request = [ASIHTTPRequest requestWithURL:url]; 4 self.request.timeOutSeconds = 10; 5 6 //设置block 7 [self.request setStartedBlock:^{ 8 NSLog(@"setStartedBlock ----"); 9 }]; 10 11 [self.request setDataReceivedBlock:^(NSData *data) { 12 NSLog(@"setDataReceivedBlock ----"); 13 }]; 14 15 [self.request setCompletionBlock:^{ 16 NSLog(@"setCompletionBlock ----"); 17 }]; 18 19 //发送请求 20 [self.request startAsynchronous];
- Block 方法就是在 block 块中实现了代理方法,也即在请求的不同状态时调用对应的 block 块
- Block 相比代理方法有一个好处就是当有多个请求时,代理方法中需要区分不同请求的数据,block 中不需要
1 //创建请求对象 2 NSURL *url = [NSURL URLWithString:@"你的 url"]; 3 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 4 5 //添加请求体中的参数 6 [request setPostValue:@"111" forKey:@"username"]; 7 [request setPostValue:@“222" forKey:@"pwd"]; 8 9 //请求完毕 10 [request setCompletionBlock:^{ 11 // 12 }]; 13 14 //发送请求 15 [request startAsynchronous];
1 NSURLRequest *request = [NSURLRequest requestWithURL: url]; 2 3 request.delegate = self; 4 5 [request setDidStartSelector: @selector( start)];
-(NSData *) responseData; //返回服务器的二进制数据 -(NSString *) responseString; //将二进制数据转换成字符串
- 下载文件:
1 //创建请求对象 2 NSURL *url = [NSURL URLWithString:@“下载 url"]; 3 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 4 5 //设置所下载文件的存储路径 6 NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 7 NSString *filepath = [cache stringByAppendingPathComponent:@“文件名"]; 8 request.downloadDestinationPath = filepath; 9 10 //发送请求 11 [request startAsynchronous];
- 想要获取下载进度需要设置 ASIProgressDelegate 代理,这里可以直接设置进度指示器view为它的代理,因为代理方法应该实现 setProgress 方法,而 UIProgressView 自带该方法:
//设置下载代理 request.downloadProgressDelegate = self.progressView;
- 支持断点下载:
//支持断点下载 request.allowResumeForFileDownloads = YES;
1 //创建请求 2 NSURL *url = [NSURL URLWithString:@“url"]; 3 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 4 5 //所要上传文件的路径 6 NSString *file = [[NSBundle mainBundle] pathForResource:@"test.txt" ofType:nil]; 7 [request setFile:file forKey:@"file"]; 8 //[request setFile:file withFileName:@"123.txt" andContentType:@"text/plain" forKey:@"file"]; 9 10 //设置其他请求参数 11 [request setPostValue:@"ifangcy" forKey:@"username"]; 12 13 //发送请求 14 [request startAsynchronous]; 15 16 //监听请求,请求完成时调用 17 [request setCompletionBlock:^{ 18 NSLog(@"上传完毕"); 19 }]; 20
1 //将文件压缩为二进制数据 2 NSData *data = UIImagePNGRepresentation(文件对象); 3 [request setData:data withFileName:@“文件名" andContentType:@"image/png" forKey:@"file"];
// 设置代理监听上传进度 request.uploadProgressDelegate = self.progressView;
ASIDownloadCache *cache = [ASIDownloadCache sharedCache];
cache.defaultCachePolicy = ASIOnlyLoadIfNotCachedCachePolicy;
[cache setStoragePath: path];
request.downloadCache = cache;
request1.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
- defaultCachePolicy:决定什么时候使用缓存,比如先网络请求后访问缓存,还是先访问缓存后网络请求等
- cacheStoragePolicy:决定存储的时间长度从而决定缓存的存储位置,ASICachePermanentlyCacheStoragePolicy 表示永久存储即存储到硬盘中, ASICacheForSessionDurationCacheStoragePolicy(默认) 表示短暂的缓存即存储到内存中
1 //获得系统默认的缓存管理对象 2 ASIDownloadCache *cache = [ASIDownloadCache sharedCache]; 3 // 默认的缓存加载策略 : 不读取缓存 4 cache.defaultCachePolicy = ASIDoNotReadFromCacheCachePolicy; 5 6 //使用缓存 7 request1.downloadCache = cache; 8 // 缓存加载策略 9 request1.cachePolicy = ASIOnlyLoadIfNotCachedCachePolicy; 10 // 缓存存储策略 11 request1.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy; 12 13 // 使用缓存 14 request2.downloadCache = cache; 15 // 缓存加载策略 16 request2.cachePolicy = ASIDontLoadCachePolicy; 17 // 缓存存储策略 18 request2.cacheStoragePolicy = ASICacheForSessionDurationCacheStoragePolicy;
//获得系统默认的缓存管理对象(决定着缓存存储路径) ASIDownloadCache *cache = [ASIDownloadCache sharedCache]; cache.defaultCachePolicy = ASIOnlyLoadIfNotCachedCachePolicy; //设置cache为全局缓存 [ASIHTTPRequest setDefaultCache:cache];
//设置缓存一天 [request1 setSecondsToCache: 60 * 60 * 24];
//判断数据是否从缓存中读取 BOOL isUseCache = [request1 didUseCachedResponse];
[ASIHTTPRequest isNetworkInUse];
[ASIHTTPRequest setShouldUpdateNetworkActivityIndicator: YES];
// 程序进入后台, 继续发送请求 request.shouldContinueWhenAppEntersBackground = YES;
request.numberOfTimesToRetryOnTimeout = 5;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。