Iphone 设置进度仅在asi http请求自定义进度跟踪中提供0和1值
我想在ASIHttprequest库上显示下载进度条(HUD进度条)。 我尝试了这个Iphone 设置进度仅在asi http请求自定义进度跟踪中提供0和1值,iphone,ios,asihttprequest,Iphone,Ios,Asihttprequest,我想在ASIHttprequest库上显示下载进度条(HUD进度条)。 我尝试了这个[request setDownloadProgressDelegate:loadingHUD]; [请求设置ShowAccurateProgress:是]; [请求setDelegate:self]; 但没有运气,它在开始时显示为空白,在结束时直接显示全部进度。 所以我记录了进度值,得到了set progress0.000000 set progress1.000000 如何获得HUD进度条的准确进度更新 我
[request setDownloadProgressDelegate:loadingHUD];
[请求设置ShowAccurateProgress:是];
[请求setDelegate:self];
但没有运气,它在开始时显示为空白,在结束时直接显示全部进度。
所以我记录了进度值,得到了set progress0.000000 set progress1.000000
如何获得HUD进度条的准确进度更新
我将Json和数据一起发送到我的服务器,服务器验证是否正确,如果可以验证,则返回视频数据作为响应。我想将该响应存储到视频文件中,所以我使用这个
这是我的密码
loadingHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
loadingHUD.labelText = NSLocalizedString(@"Downloading", nil);
loadingHUD.mode=MBProgressHUDModeAnnularDeterminate;
[loadingHUD showWhileExecuting:@selector(verifyReceipt:) onTarget:self withObject:transaction animated:YES];
// call the verify receipt method and download the file
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void)verifyReceipt:(SKPaymentTransaction *)transaction {
// string to NSdictionary and dictioanry to json string
NSArray *mainObjects = [NSArray arrayWithObjects:vendorId,deviceData,productID,nil]; // purchased Item ID of previous item
NSArray *mainKeys = [NSArray arrayWithObjects:@"ID",@"device",@"video",nil];
NSDictionary *mainDictionary = [NSDictionary dictionaryWithObjects:mainObjects forKeys:mainKeys]; // final string of data
NSLog(@"Json Main dict created");
NSString *sandBox=@"1";
NSArray *testobj = [NSArray arrayWithObjects:mainDictionary,receiptDataEncoded,sandBox,nil]; // purchased Item ID of previous item
NSArray *testkey = [NSArray arrayWithObjects:@"data",@"receiptData",@"sandbox",nil];
NSDictionary *testdict = [NSDictionary dictionaryWithObjects:testobj forKeys:testkey]; // final string of data
NSLog(@"Json Main dict created");
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:testdict options:NSJSONWritingPrettyPrinted error:&error];
NSString *resultAsString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"Purchase product Json string:\n%@", resultAsString);
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[[NSURL alloc] initWithString:@"http://testing.test.io/dev.php/video/verifyReceipt"]];
//[request setDidReceiveDataSelector:@selector(request:didReceiveBytes:)];
[request setPostValue:resultAsString forKey:@"verify"];
[request setDidFinishSelector:@selector(requestDone:)];
[request setTimeOutSeconds:120];
[request setNumberOfTimesToRetryOnTimeout:2];
// request.showAccurateProgress=YES;
[request setDownloadProgressDelegate:loadingHUD];
[request setShowAccurateProgress:YES];
request setDelegate:self];
//[request setDownloadProgressDelegate:self];
// SAVED video PATH
// Get the Document directory
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// Add your filename to the directory to create your saved pdf location
NSString *movLocation = [documentDirectory stringByAppendingPathComponent:[fileName st ringByAppendingString:@".mov"]];
// TEMPORARY video PATH
// Get the Caches directory
NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// Add your filename to the directory to create your temp pdf location
NSString *tempMovLocation = [cachesDirectory stringByAppendingPathComponent:[fileName stringByAppendingString:@".mov"]];
// Tell ASIHTTPRequest where to save things:
[request setTemporaryFileDownloadPath:tempMovLocation];
[request setDownloadDestinationPath:movLocation];
[request startSynchronous];
NSLog(@"Path is %@",movLocation);
}
- (void)setProgress:(float)progress1
{
[loadingHUD setProgress:progress1];
NSLog(@"set progress%f",progress1);
}
- (void)requestDone:(ASIHTTPRequest *)request{
//[MBProgressHUD hideHUDForView:self.view animated:YES];
if(request.responseStatusCode==200)
{
//BOOL success = [videoData writeToFile:movLocation atomically:NO];
// NSLog(@"bool value is%d",success);
NSLog(@"in request done sucsessfully downlaod and store in database %d",request.responseStatusCode);
[DBHelper savePurchaseId:fileName];
[self movieReceived];
}
else{
NSLog(@"in request downlaod and store in database failed %@",request.responseHeaders);
}
}
-(void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"%@",request.error);
}
如果我使用didreceivebytes,我的文件将无法下载
-(void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes {
NSLog(@"Received bytes:%lld",bytes); progress = 0.0f; if (progress < 1.0f)
{
progress = ((float)bytes) / 469690;
NSLog(@"progress value is %f",progress);
loadingHUD.progress = progress;
}
-(void)请求:(ASIHTTPRequest*)请求didReceiveBytes:(long-long)字节{
NSLog(@“接收字节:%lld”,字节);进度=0.0f;如果(进度<1.0f)
{
进度=((浮点)字节)/469690;
NSLog(@“进度值为%f”,进度);
loadingHUD.progress=进度;
}
}
非常感谢您的帮助我使用afnetworking解决了这个问题 以及在字典中重新记录setparameterencoding和添加post值
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// Add your filename to the directory to create your saved pdf location
NSString* destPath = [documentDirectory stringByAppendingPathComponent:[fileName stringByAppendingString:@".mov"]];
NSURL *url = [NSURL URLWithString:@"http:/testing.io"]; //
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
// [httpClient setParameterEncoding:AFJSONParameterEncoding];// no need of this line remove
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"dev.php/video/verifyReceipt" parameters:postRequest]; // /dev.php/video/verifyReceipt
NSLog(@"postRequest: %@", postRequest);
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:destPath append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
// Give alert that downloading successful.
NSLog(@"Successfully downloaded file to %@", destPath);
// [self.target parserDidDownloadItem:destPath];
HUD.detailsLabelText = [NSString stringWithFormat:@"%@ %i%%",@"Downloading",100];
[HUD hide:TRUE];
[DBHelper savePurchaseId:fileName];
[self movieReceived];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
// Give alert that downloading failed
NSLog(@"Error: %@", error);
// [self.target parserDidFailToDownloadItem:error];
[HUD hide:TRUE];
}];
[operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
{
// Progress
float totalProgress = (totalBytesWritten / (totalBytesExpectedToWrite * 1.0f) * 100);
HUD.detailsLabelText = [NSString stringWithFormat:@"Downloading %i%%", MIN((int)(totalProgress), 99)];
}];
[operation start];
}您以前也发布过类似的问题,我已经回答了。你试过了吗?@Adithya我的问题没有解决,这就是为什么你得到了我提供的解决方案?@Adithya我解决了我的问题。这是因为两个原因。第一个是在Asithtep中,我正在使用setpostvalue和afnetworking,我在ndDictionAnry中添加该post值,第二个是删除[httpClient SetParameterEncode:AFJSonParameterEncode];在afnetworking中