关于使用ASIHTTPRequest进行断点续传
2015-09-19 13:56
1026 查看
最近项目中用到了ASI的断点续传,把其中遇到的问题和解决方法与大家分享
解决方案如下:找到ASIHTTPRequest的.m文件
NSString *bookPath = [RBUtilsObject filePathString:s9id]; ASIHTTPRequest *bookDownloadRequest = [[ASIHTTPRequest alloc]initWithURL:requestURL]; bookDownloadRequest.shouldContinueWhenAppEntersBackground = YES; [bookDownloadRequest setDownloadDestinationPath:bookPath]; NSString *tmpbookPath = [RBUtilsObject tmpfilePathString:s9id]; [bookDownloadRequest setTemporaryFileDownloadPath:tmpbookPath]; [bookDownloadRequest setAllowResumeForFileDownloads:YES]; bookDownloadRequest.allowCompressedResponse = NO; __block NSString *currentDownloadBookID = s9id; __block BOOL isAddToShelf = NO; [bookDownloadRequest setBytesReceivedBlock:^(unsigned long long size, unsigned long long total) { if ( total !=0) { if (isAddToShelf == NO) { isAddToShelf = YES; [[NSNotificationCenter defaultCenter]postNotificationName:@(kIsAddToShelf) object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:currentDownloadBookID,@(kCurrentDownloadBookID), nil]];//s9id } float progressAmount = (float)((size*1.0)/(total*1.0)); NSLog(@"downloadPercent=%f s9id= %@",progressAmount,currentDownloadBookID); //这里的size其实是request接收到的数据的回调block,并不是当前全部已下载的数据,因此在这样不能正确显示下载进度 [[NSNotificationCenter defaultCenter]postNotificationName:currentDownloadBookID object:@(progressAmount)]; } }];
解决方案如下:找到ASIHTTPRequest的.m文件
- (void)updateDownloadProgress { // We won't update download progress until we've examined the headers, since we might need to authenticate if (![self responseHeaders] || [self needsRedirect] || !([self contentLength] || [self complete])) { return; } unsigned long long bytesReadSoFar = [self totalBytesRead]+[self partialDownloadSize]; unsigned long long value = 0; if ([self showAccurateProgress] && [self contentLength]) { value = bytesReadSoFar-[self lastBytesRead]; if (value == 0) { return; } } else { value = 1; [self setUpdatedProgress:YES]; } if (!value) { return; } [ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:&queue withObject:self amount:&value callerToRetain:self]; [ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:&downloadProgressDelegate withObject:self amount:&value callerToRetain:self]; [ASIHTTPRequest updateProgressIndicator:&downloadProgressDelegate withProgress:[self totalBytesRead]+[self partialDownloadSize] ofTotal:[self contentLength]+[self partialDownloadSize]]; #if NS_BLOCKS_AVAILABLE if (bytesReceivedBlock) { unsigned long long totalSize = [self contentLength] + [self partialDownloadSize]; // [self performBlockOnMainThread:^{ if (bytesReceivedBlock) { bytesReceivedBlock(value, totalSize); }}];此处修改为即可将下载进度正确返回 [self performBlockOnMainThread:^{ if (bytesReceivedBlock) { bytesReceivedBlock(([self totalBytesRead] + [self partialDownloadSize]),totalSize); } }]; } #endif [self setLastBytesRead:bytesReadSoFar]; }
相关文章推荐
- iOS安全系列之 HTTPS 进阶
- 浏览器和服务器在基于https进行请求链接到数据传输过程中,用到的技术
- XCode7使用AFNetworking无法访问网络解决办法
- httpd服务配置(未完待续)
- 小书签 - 翻译
- 网络调试命令nc命令使用详解
- 计算机网络之面试常考
- Xcode7网络报错:App Transport Security has blocked a cleartext HTTP
- iOS-ASIHTTPRequest缓存机制
- Android与server通信中的方法(TCP)高效的安全完整
- 网络笔记02数据解析 -1-JSON解析
- 《TCP/IP详解卷1:协议》——第6章 ICMP:Internet控制报文协议(转载)
- HTTP和socket的区别
- Linux下用netstat命令查看网络负载状况
- 技术人生:故事之十 网络是什么?
- nefu494深海机器人问题【网络流24题】最大费用流
- httpclient简单学习(一)
- 配置NAT网络
- Java知识整理(十)之SE.TCP通信(Server和Client源代码)
- TCP/IP的三次握手,四次分手。