gpt4 book ai didi

ios - 这个 Web 服务调用代码是什么,到底发生了什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:55:50 24 4
gpt4 key购买 nike

<分区>

下面的代码确实调用了一个网络服务,但我不完全理解它的结构。

NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject: requestData options:0 error:&error];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"private" forHTTPHeaderField:@"Cache-Control"];
[request setValue:@"Microsoft-IIS/7.5" forHTTPHeaderField:@"Server"];
[request setValue:@"Basic realm='TimeBoxWebAPI'" forHTTPHeaderField:@"WWW-Authenticate"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if ([data length] >0 && error == nil){
dispatch_queue_t myQueue = dispatch_queue_create("My Queue", DISPATCH_QUEUE_SERIAL);

dispatch_async(myQueue, ^{

[self parseGetSchedulingData:data];
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[tableHolidays reloadData];
[refreshControl endRefreshing];

if(activityIndicator.isAnimating)
{
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
}
});
});
}
else if ([data length] == 0 && error == nil){
NSLog(@"Empty Response, not sure why?");

[[[UIAlertView alloc] initWithTitle: nil
message:@"Error while trying to log in. Please try later"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];

dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if(activityIndicator.isAnimating)
{
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
}
});

}


else if (error != nil){
NSLog(@"%@", error.description);
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if(activityIndicator.isAnimating)
{
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
}
});
}
}];
}

特别是,我不确定对 setValue:forHTTPHeaderField:sendAsynchronousRequest:queue:completion: 的调用到底做了什么。

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