//  AppDelegate.m
// UI2_异步下载
//
// Created by zhangxueming on 15/7/17.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *root = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController = nav;
self.window.backgroundColor = [UIColor whiteColor]; return YES;
}
//  ViewController.h
// UI2_异步下载
//
// Created by zhangxueming on 15/7/17.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UITableViewController <NSURLConnectionDataDelegate> @end //
// ViewController.m
// UI2_异步下载
//
// Created by zhangxueming on 15/7/17.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "ViewController.h"
#import "UIImageView+WebCache.h" //异步下载:向服务器发送请求后,UI主线程能继续交互,另外单独创建一个线程下载数据 @interface ViewController ()
{
NSURLConnection *_urlConntecion;//建立客户端与服务器的连接
NSMutableArray *_dataList; //数据源
NSMutableData *_receiveData; //存储服务器下发的数据
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
_dataList = [NSMutableArray array];
_receiveData = [NSMutableData data];
[self loadDataWithPage:10];
} - (void)loadDataWithPage:(NSInteger)page
{
//1.创建NSURL
NSString *urlString = [NSString stringWithFormat:@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=%ld", page];
NSURL *url = [NSURL URLWithString:urlString]; //2.创建网络请求对象
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; //3.通过NSURLConnection对象实现异步下载数据
_urlConntecion = [[NSURLConnection alloc] initWithRequest:request delegate:self];
} #pragma mark ---NSURLConnectionDataDelegate--- //当客户端接受到服务器的响应,调用此方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//打印状态码
NSLog(@"statusCode = %li", ((NSHTTPURLResponse *)response).statusCode);
//清空数据
[_receiveData setLength:0];
} //当从服务器接受到数据的时候, 调用此方法,如果数据量比较大, 该方法被调用多次
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_receiveData appendData:data];
} //当数据完成下载, 调用该方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//数据解析
id result = [NSJSONSerialization JSONObjectWithData:_receiveData options:NSJSONReadingMutableContainers error:nil];
if ([result isKindOfClass:[NSDictionary class]])
{
NSLog(@"result = %@", result);
NSArray *app = [result objectForKey:@"applications"];
[_dataList addObjectsFromArray:app];
}
else if([result isKindOfClass:[NSArray class]])
{ }
//刷新UI
[self.tableView reloadData];
} //下载数据失败的时候调用该方法
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//打印错误信息
NSLog(@"%@", error.localizedDescription);
} #pragma mark ---UITableViewDataSource--- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataList.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseId = @"cellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseId];
}
//name
//currentPrice
//iconUrl
NSDictionary *dict = _dataList[indexPath.row];
cell.textLabel.text = [dict objectForKey:@"name"];
cell.detailTextLabel.text =[NSString stringWithFormat:@"原价:%@RMB",[dict objectForKey:@"lastPrice"]];
NSString *iconUrl = [dict objectForKey:@"iconUrl"];
//图片异步加载并设置默认图片
[cell.imageView setImageWithURL:[NSURL URLWithString:iconUrl] placeholderImage:[UIImage imageNamed:@"holderImage"]]; return cell;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

UI2_异步下载的更多相关文章

  1. Android多线程分析之五:使用AsyncTask异步下载图像

    Android多线程分析之五:使用AsyncTask异步下载图像 罗朝辉 (http://www.cnblogs.com/kesalin) CC 许可,转载请注明出处 在本系列文章的第一篇<An ...

  2. Android多线程分析之一:使用Thread异步下载图像

    Android多线程分析之一:使用Thread异步下载图像 罗朝辉 (http://www.cnblogs.com/kesalin) CC 许可,转载请注明出处   打算整理一下对 Android F ...

  3. WebClient.DownloadFile(线程机制,异步下载文件)

    线程机制(避免卡屏),异步下载文件. 我做网站的监控,WebClient.DownloadFile这个方法是我经常用到的,必要的时候肯定是要从网上下载些什么(WebRequest 也可以下载网络文件, ...

  4. iOS异步下载下载进度条显示

    说到http异步下载,首先要知道其中的关键类. 关键类是NSURLConnection  NSURLRequest NSMutableURLRequest  委托是 NSURLConnectionDo ...

  5. unity下载文件三(http异步下载)

    异步下载,顾名思义就是不影响你主线程使用客户端的时候,人家在后台搞你的明堂. 直接入主题,既然要下载,首先得请求,请求成功之后进行回调,这就是一个异步过程,异步回调的时间不可控. 1.首先请求下载. ...

  6. Android 中的异步下载

    网上提到最多的就是利用AsyncTask进行异步下载,用android-async-http第三方库的也比较多.这里写点注意事项. 先说说android-async-http,这个库发送请求利用thr ...

  7. FtpWebRequest FTP异步下载、异步上传文件

    异步下载: public interface IPrimaryKey<T> { T GetKey(); } public class DownloadInfo : IPrimaryKey& ...

  8. Android异步下载图片并且缓存图片到本地

    Android异步下载图片并且缓存图片到本地 在Android开发中我们经常有这样的需求,从服务器上下载xml或者JSON类型的数据,其中包括一些图片资源,本demo模拟了这个需求,从网络上加载XML ...

  9. android AsyncTask异步下载并更新进度条

    AsyncTask异步下载并更新进度条    //如果不是很明白请看上篇文章的异步下载 AsyncTask<String, Integer, String> 第一个参数:String 传入 ...

随机推荐

  1. 《HTTP2基础教程》笔记

    <HTTP2基础教程>笔记 HTTP/1问题 队头阻塞 低效TCP 慢启动 拥塞避免阶段 臃肿头部 受限的优先级 高优先级无法插队 第三方资源 h2也无法很好解决 web性能优化 DNS查 ...

  2. TextBox的OnTextboxChanged事件里对Text重新赋值带中文, 导致崩溃

    今天遇到一个超级bug, Textbox做了限制, 只能输入数字. 结果在搜狗输入法输入中文时导致崩溃, 出错信息如下: 未处理 System.InvalidOperationException   ...

  3. vijos:P1053Easy sssp(spfa判环)

    描述 输入数据给出一个有N(2 <= N <= 1,000)个节点,M(M <= 100,000)条边的带权有向图. 要求你写一个程序, 判断这个有向图中是否存在负权回路. 如果从一 ...

  4. C# FileStream Write追加写入文本

    该例子为追加 C盘中的 file1.txt 的文本内容 完整代码如下: 引入命名空间: [csharp] view plain copy print? using System.IO; 完整代码: [ ...

  5. ng2中的ng-content用法

    用途:1.ng-content用于在组件中嵌入内容 2.ng-content可以在组件中嵌入模板代码,方便定制可复用的组件 select属性支持css选择器,如"#id",&quo ...

  6. python2 + selenium + eclipse 中,通过django生产数据库表的时候报错

    python2 + selenium + eclipse 中,通过django生产数据库表的时候报错 解决: 1.查看自己电脑中,“开始-->控制面板-->管理工具-->服务--&g ...

  7. mysql两主多从

    1.实现目标 目标清单: 1)Master(192.168.31.230)为正常运行环境下的主库,为两个Slave(192.168.31.231和192.168.31.232)提供“主-从”复制功能: ...

  8. JavaScript高级程序设计学习笔记第十四章--表单

    1.在 HTML 中,表单是由<form>元素来表示的,而在 JavaScript 中,表单对应的则是 HTMLFormElement 类型. HTMLFormElement 继承了 HT ...

  9. Git的使用 强制放弃本地所有修改,获取master中最新版本更新本地

    git fetch --all git reset --hard origin/master git fetch --all 的意思是,下载远程库的所有内容,但不与本地做任何合并 git reset ...

  10. htons和htonl函数具体应用

    htons和htonl函数具体应用 htons和htonl函数,是用来将主机字节顺序转换为网络字节顺序在进行网络抓包时,抓到的包的数据是网络字节顺序,在进行编程时,要进行主机字节顺序和网络字节顺序间的 ...