有人知道如何等待http请求的响应吗?在我的代码中,我正在对 url 进行 http 请求,然后我需要做的是检查 http 响应以决定不同的处理方式。我有这样的事情:
-(void)check{
[self fetchURL:@"http://something"];
if(response != nil || [response length] != 0){
do something....
}
else{
do something else....
}
}
-(void)fetchURL:(NSString *)urlWeb{
NSURL *url = [NSURL URLWithString:urlWeb];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[connection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"INSIDE OF didReceiveResponse");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"INSIDE OF didFailWithError");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"INSIDE OF connectionDidFinishLoading");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
NSLog(@"inside of didReceiveData");
response = [NSString stringWithUTF8String:[data bytes]];
NSLog(@"response: %@", response);
}
我一直在尝试在这里看到的不同选项,但我无法停止执行我的代码并等待那个答案......这意味着当我检查我的 http 请求的响应时,它总是显示为空或为零参考...任何帮助如何弄清楚?谢谢