嗨,我是ios的初学者,当我们使用NSURLRequest调用服务时,我想知道我想知道当我们通过“同步请求”调用服务并以编程方式调用异步请求时,会发生什么情况,
请以编程方式说明该操作,并且我在下面使用该代码编写了一些代码,以解释同步和异步操作
我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"GET"];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[theRequest setTimeoutInterval:5];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(connection){
webData = [[NSMutableData alloc] init];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[webData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[webData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"error is %@",[error localizedDescription]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString * allDataDictionbary = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
NSArray * responseString = [allDataDictionbary JSONValue];
NSLog(@"final respone dictionary%@",responseString);
}
最佳答案
为了回答您的问题,同步请求会阻塞从中调用它们的线程,直到请求完成为止。 (并且由于这个原因,通常不鼓励使用同步请求。)异步请求使当前线程在执行请求时继续执行(例如,继续响应用户与应用程序的交互;响应系统事件等)。这通常是优选的。
您的代码正在执行异步请求(很好)。但是有一些问题:
NSURLConnection
。不推荐使用。使用NSURLSession
。它实际上更简单,因为您通常不必编写那些委托方法(除非您要这样做,因为您迫切需要这样做)。有关
NSURLSession
的更多信息,请参见《 URL session 编程指南》中的Using NSURLSession。或参阅WWDC 2013视频What's New in Foundation Networking获得不错的介绍。NSError
对象,而可能只是导致HTTP状态码不是200我建议检查一下。有关HTTP状态代码的列表,请参见RFC 2616的第10节。
Content-Type
设置为application/json
。但这不是JSON请求。 (当然,响应是JSON,但请求不是。)通常,您会对这种请求使用application/x-www-form-urlencoded
。NSArray
作为顶级对象。但是顶层对象是NSDictionary
。JSONValue
。我不确定是哪个JSON库,但是我们许多人只是使用Apple提供的内置 NSJSONSerialization
类。很久以前,在Apple提供NSJSONSerialization
之前,我们将使用第三方库来解析JSON,但这不再是必需的。使用
NSURLSession
发送请求的正确方法如下:NSURL *url = [NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error != nil) {
NSLog(@"fundamental network error = %@", error);
return;
}
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200) {
NSLog(@"Warning; server should respond with 200 status code, but returned %ld", (long)statusCode);
}
}
NSError *parseError;
NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
if (responseObject) {
NSLog(@"responseObject = %@", responseObject);
} else {
NSLog(@"Error parsing JSON: %@", parseError);
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"responseString = %@", responseString);
}
}];
[task resume];
// Note, you'll reach this portion of your code before the
// above `completionHandler` runs because this request runs
// asynchronously. So put code that uses the network response
// above, inside that `completionHandler`, not here.