以下代码确实调用了Web服务,但我并不完全理解它的结构。
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:
的确切内容。
答案 0 :(得分:2)
正如the documentation所说,
参数
请求
要加载的网址请求。作为初始化过程的一部分,请求对象被深度复制。此方法返回后对请求所做的更改不会影响用于加载过程的请求。
队列
请求完成或失败时将处理程序块分派到的操作队列。
处理程序
要执行的处理程序块。
讨论
如果请求成功完成,则处理程序块的data参数包含资源数据,error参数为nil。如果请求失败,则data参数为nil,error参数包含有关失败的信息。
如果需要进行身份验证才能下载请求,则必须将所需的凭据指定为URL的一部分。如果身份验证失败或缺少凭据,则连接将尝试在没有凭据的情况下继续。如果请求以401 Unauthorized状态代码结束,则response参数为nil,data参数包含资源数据,error参数为NSURLErrorDomain错误域中NSURLErrorUserCancelledAuthentication代码的NSError。
状况
适用于iOS 5.0及更高版本。
在iOS 9.0中不推荐使用。
如果仍然不清楚,请告诉我。
修改1 :
关于
[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];
为HTTP header fields设置了不同的值,最后为请求设置了HTTP正文。