gpt4 book ai didi

iphone - 如何避免 iPhone 中的 Web 服务(ASIHTTPRequest)响应延迟?

转载 作者:行者123 更新时间:2023-11-29 03:44:21 25 4
gpt4 key购买 nike

在我的 iPhone 应用程序中处理用于存储和检索数据的 Web 服务。现在我使用以下代码进行 Web 服务处理。

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

[request setPostValue:@"1" forKey:@"id"];

[request setTag:100];

[request setDelegate:self];

[request startAsynchronous];

通过这段代码,我在“requestFinished”方法中得到了响应。我的问题是网络服务响应非常延迟(取决于互联网速度)。如何使网络服务响应非常快?请帮助我。

最佳答案

我认为你想通过post方法发送json对象..延迟取决于你的服务器(它处理请求和响应的速度),但我建议你使用进度条和 block 来处理网络请求..

loadingHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
loadingHUD.labelText = NSLocalizedString(@"Downloading", nil);
loadingHUD.mode=MBProgressHUDModeAnnularDeterminate;
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];
// Add your filename to the directory to create your saved file location
NSString* destPath = [documentDirectory stringByAppendingPathComponent:[fileName stringByAppendingString:@".mov"]];
NSURL *url = [NSURL URLWithString:mainURL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:postURL parameters:postRequest];
NSLog(@"postRequest: %@", postRequest);

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:destPath append:NO];



[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{

NSLog(@"Successfully downloaded file to %@",[[NSString alloc] initWithData:operation.responseData encoding:NSASCIIStringEncoding]);

// Give alert that downloading successful.
NSLog(@"Successfully downloaded file to %@", destPath);

NSLog(@"response: %@", operation.responseString); // Give alert that downloading successful.

// [self.target parserDidDownloadItem:destPath];

loadingHUD.detailsLabelText = [NSString stringWithFormat:@"%@ %i%%",@"Downloading",100];
[loadingHUD hide:TRUE];

[DBHelper savePurchaseId:fileName];
[self movieReceived];

}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
// Give alert that downloading failed
NSLog(@"Error: %@", error);

// [self.target parserDidFailToDownloadItem:error];
[loadingHUD hide:TRUE];

}];
[operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
{
// Progress
progress = ((float)totalBytesWritten) / fileSize;
loadingHUD.progress = progress;
}];
[operation start];

}

关于iphone - 如何避免 iPhone 中的 Web 服务(ASIHTTPRequest)响应延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17939949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com