iOS--异步下载
#import "ViewController.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()
{
NSURLConnection *_connection;
NSMutableData *_data;
NSMutableArray *_dataList;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.view.backgroundColor=[UIColor cyanColor];
// Do any additional setup after loading the view, typically from a nib.
_data=[NSMutableData data];
_dataList =[NSMutableArray array];
[self loadDataWithPage:1];
}
-(void)loadDataWithPage:(NSInteger)pageIndex
{
//1.创建NSURL对象
NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"http://iappfree.candou.com:8080/free/applications/limited//?currency=rmb&page=%li",pageIndex]];
//2.创建网络请求
NSURLRequest *request =[[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10];
//3.创建NSURLConnection,通过代理下载数据,遵守<NSURLConnectionDataDelegate>代理
_connection =[NSURLConnection connectionWithRequest:request delegate:self];
}
#pragma mark ----NSURLConnectionDataDelegate协议方法-------
//1.接受完HTTP协议头,开始真正接受数据时调用,一般在这个方法里初始化一些存储数据的对象,如:NSMutableData
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse =(NSHTTPURLResponse *)response;
//
NSLog(@"%@",httpResponse.allHeaderFields);
//打印状态码,200请求成功。404表示请求数据失败,403表示服务器错误
//NSLog(@"%li",httpResponse.statusCode);
//清空数据,准备接受新的数据
[_data setLength:0];
}
//接收到数据后,调用此方法,此方法可能被调用多次
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//追加数据
[_data appendData:data];
}
//数据下载完成时被调用
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//解析数据
if (_data) {
id Json =[NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil];
if ([Json isKindOfClass:[NSDictionary class]]) {
[_dataList addObjectsFromArray:Json[@"applications"]];//copy,也可以
}
NSLog(@"%@",_dataList);
//重新加载tableView
[self.tableView reloadData];
}
}
//数据请求失败时调用
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//打印错误信息
NSLog(@"%@",error.localizedDescription);
}
#pragma mark ------UITableViewDelegateSourse----
-(NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio
{
return _dataList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID =@"cellID";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
cell.textLabel.text=[_dataList[indexPath.row]objectForKey:@"name"];
cell.detailTextLabel.text=[NSString stringWithFormat:@"下载次数:%@",_dataList[indexPath.row][@"downloads"]];
//iconUrl = "http://photo.candou.com/i/114/8c1b645b8d110973b14b9934315ce970";
NSURL *iconUrl =_dataList[indexPath.row][@"iconUrl"];
[cell.imageView setImageWithURL:iconUrl placeholderImage:[UIImage imageNamed:@"lqcq.jpg"] ];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
iOS--异步下载的更多相关文章
- iOS异步下载下载进度条显示
说到http异步下载,首先要知道其中的关键类. 关键类是NSURLConnection NSURLRequest NSMutableURLRequest 委托是 NSURLConnectionDo ...
- ios专题 - 异步下载加下载进度显示
[罗国强原创] 今天被刺激了,愤概地要写下这边博文. 说到http异步下载,首先要知道其中的关键类. 关键类是NSURLConnection NSURLRequest NSMutableURLReq ...
- IOS GCD图片数据异步下载,下载完成后合成显示
关于GCD使用详解,请看我的上一篇blog:http://www.cnblogs.com/xin-lang/p/6278606.html 前段时间遇到个需要异步下载,下载完成后再组合显示的东西.这里我 ...
- 【iOS系列】-多图片多线程异步下载
多图片多线程异步下载 开发中非常常用的就是就是图片下载,我们常用的就是SDWebImage,但是作为开发人员,不仅要能会用,还要知道其原理.本文就会介绍多图下载的实现. 本文中的示例Demno地址,下 ...
- Android 中的异步下载
网上提到最多的就是利用AsyncTask进行异步下载,用android-async-http第三方库的也比较多.这里写点注意事项. 先说说android-async-http,这个库发送请求利用thr ...
- iOS异步图片加载优化与常用开源库分析
网络图片显示大体步骤: 1.下载图片: 2.图片处理(裁剪,边框等): 3.写入磁盘: 4.从磁盘读取数据到内核缓冲区: 5.从内核缓冲区复制到用户空间(内存级别拷贝): 6.解压缩为位图(耗cpu较 ...
- [Xcode 实际操作]八、网络与多线程-(22)使用GCD多线程技术异步下载图片
目录:[Swift]Xcode实际操作 本文将演示如何使用使用GCD多线程技术异步下载图片. Grand Central Dispatch(GCD) 是 Apple 开发的一个多核编程的较新的解决方法 ...
- 关于ios异步加载图片的几个开源项目
一.HjCache 原文:http://www.markj.net/hjcache-iphone-image-cache/ 获取 HJCache: HJCache is up on github h ...
- Delphi10.2.3利用THttpClient实现http异步下载
随着Delphi 10.2.3的发布,随之带来更稳定.更完善的版本.今天借官方的例子,解读一下如何实现Http异步下载并显示下载进度. 使用的核心组件是THttpClient,首先建立一个THttpC ...
- Android多线程分析之五:使用AsyncTask异步下载图像
Android多线程分析之五:使用AsyncTask异步下载图像 罗朝辉 (http://www.cnblogs.com/kesalin) CC 许可,转载请注明出处 在本系列文章的第一篇<An ...
随机推荐
- DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 的使用已经过时!
DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 的使用已经过时! 请使用..spring 3.1 开始我们应该用 Re ...
- arcgis engine 监听element的添加、更新和删除事件(使用IMovePointFeedback)
VB代码: 复制进程序稍作修改变量名和事件逻辑即可使用. Members AllPropertiesMethodsInheritedNon-inherited Description Displa ...
- python Windows下的android设备截图工具
目录 界面版 命令行版 界面版 利用python的wx库写个ui界面,用来把android设备的截图输出到电脑屏幕,前提需要安装adb,涉及到的python库也要安装.代码如下: #!/usr/bin ...
- Zabbix3 agent端安装(二)
1.基础环境准备 安装zabbix的yum源,这里有必要提一点,阿里的yum源已经提供了zabbix3.0 1.1.yum源配置 rpm -ihv http://mirrors.aliyun.com/ ...
- [Java] Java反射
首先推荐三个十分有趣的网站: http://www.programcreek.com/simple-java/ http://tutorials.jenkov.com/ http://www.meet ...
- [Scala] Scala基础知识
Object An object is a type of class that can have no more than one instance, known in object-oriente ...
- ReactiveCocoa源码拆分解析(三)
(整个关于ReactiveCocoa的代码工程可以在https://github.com/qianhongqiang/QHQReactive下载) 这一章节主要讨论信号的“冷”与“热” 在RAC的世界 ...
- php项目整理之no1
1.login.php <head></head> 2.check_login.php 3.user.php 4.getData.php
- python之路九
paramiko paramiko模块是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接 ssh执行命令: import paramikossh = pa ...
- jquery写简单的div切换
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...