// 用NSURl 来表示一个URL,也就是链接
NSURL*url = [NSURL URLWithString:@"http://10.0.8.8/sns/my/user_list.php"];
//创建一个 http请求
//第一个参数是NSURL
//第二个参数是缓存策略
//第三个参数是超时时间 移动网络下,超时时间设置为60s
NSURLRequest*urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
//响应
NSURLResponse*response = nil;
//存放错误信息的
NSError*error = nil;
//发起请求 NSURLConnection Syn 同步 Async 异步
NSData*data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
//异步请求,NSURLConnection 在发起异步请求的时候,会重新建立一个线程去下载数据,所以不会卡UI
//1.urlRequest
//2.下载的代码块放在哪一个线程,UI线程(主线程)[NSOperationQueue mainQueue]// 获取主线程队列
[NSURLConnectionsendAsynchronousRequest:urlRequest queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse * _Nullableresponse, NSData * _Nullabledata, NSError * _NullableconnectionError) {
NSLog(@"下载完成,得到二进制数据");
//增加下拉刷新
UIRefreshControl*refresh = [[UIRefreshControl alloc]init];
[refresh addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:refresh];
self.refresh = refresh;
- (void)refresh:(UIRefreshControl*)sender{
//判断刷新控件的状态
if(sender.isRefreshing) {
//清空数据
[self.dataSource removeAllObjects];
//重新请求
[self loadDataSource];
//结束刷新
[sender endRefreshing];
//刷新界面
[self.tableView reloadData];
}
NSLog(@"下拉刷新");
}
//结束刷新
[self.refresh endRefreshing];
//取出数据 model
UserModel*user = self.dataSource[indexPath.row];
//拼出图片的地址
NSString*headImagUrlStr = [NSString stringWithFormat:@"http://10.0.8.8/sns%@",user.headimage];
//同步的请求
NSData*data = [NSData dataWithContentsOfURL:[NSURL URLWithString:headImagUrlStr]];
cell.imageView.image = [UIImageimageWithData:data];
[NSURLConnectionsendAsynchronousRequest:reuqest queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse * _Nullableresponse, NSData * _Nullabledata, NSError * _NullableconnectionError) {
// main 程序的主线程,所有的UI操作都在主线程。
//同步的请求如果请求持续的时间比较长(耗时操作),会卡死界面.(在主线程里面执行)
//异步的请求做耗时操作时,不会卡死界面。(因为没有在主线程里面运行)
//iOS8以后的
// 用 NSURl 来表示一个URL
//get 请求,get加参数直接在URL里面添加
//参数从哪里获得,1找接口文档 2.问服务端人员
// page 从哪一页开始
//number 每页的个数 一般一页 10 -20 数据就可以
NSURL*url = [NSURL URLWithString:@"http://10.0.8.8/sns/my/user_list.php?page=2&number=10"];
//初始化,单例(share开头的方法)
NSURLSession*session = [NSURLSession sharedSession];
//数据下载任务
NSURLSessionDataTask*sessinDataTask = [session dataTaskWithURL:urlcompletionHandler:^(NSData* _Nullable data, NSURLResponse* _Nullable response, NSError * _Nullableerror) {
//数据下载完成
if(error == nil) {
//解析数据
NSDictionary* jsonObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"json解析数据 %@",jsonObj);
//如果我们跟服务器人员交流的话,要要求他们返回什么样类型的数据
NSNumber*count = jsonObj[@"count"];
NSString*totalcount = jsonObj[@"totalcount"];
NSArray*users = jsonObj[@"users"];
NSLog(@"%ld",users.count);
//加载数据源
for(NSDictionary *dictItem in users) {
UserModel*user = [UserModel new];
user.username= dictItem[@"username"];
user.headimage= dictItem [@"headimage"];
user.uid= dictItem [@"uid"];
[self.dataSource addObject:user];
}
[self.tableView reloadData];
}
}];
//启动下载
[sessinDataTask resume];
}