第一步
/*************异步请求的代理方法[start]****************/
-(void)toRequestData:(NSString *)user_id
{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
//第一步,创建url
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@/%@?access_token=%@&friend_id=%@", service_url, _getFriendInfo, user_id, access_token, user_id]];
//第二步,创建请求
NSURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
//第三步,连接服务器
connectionInfo = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connectionInfo start];
}
第二步
#pragma mark 网络连接出错时调用
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[MBProgressHUD hideHUDForView:self.view animated:YES];
[MBProgressHUD showHUD:self text:[error localizedDescription]];
}
第三步
#pragma mark 服务器的数据已经接收完毕时调用
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[MBProgressHUD hideHUDForView:self.view animated:YES];