NSURLConnection的使用
2013-11-24 21:55
302 查看
NSURLConnection用于网络连接,这个是对于异步连接的另外一种实现方法。这种方法能够在异步连接获取数据时进行若干次的进度获知。
NSURLConnection需遵循NSURLConnectionDataDelegate协议,协议必须得实现方法包括:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
#import "ViewController.h"
#define URL02 @"http://ww3.sinaimg.cn/bmiddle/c8dc68bajw1eaw9bpj6kfj20by0bymyw.jpg"
@interface
ViewController ()
@end
@implementation ViewController
- (void)sendAsyn1:(NSString *)urlStr
{
NSString *path =
URL02;
NSURL *url = [NSURL
URLWithString:path];
NSURLRequest *request = [[NSURLRequest
alloc] initWithURL:url];
[NSURLConnection
connectionWithRequest:request delegate:self];
NSLog(@"发起异步连接");
}
- (void)viewDidLoad
{
[super
viewDidLoad];
//创建一个UIImageView框架用于放图片
self.imgV = [[UIImageView
alloc] initWithFrame:CGRectMake(0,
0, 320,
300)];
//测试是否创建成功 设置它的颜色为红色
self.imgV.backgroundColor = [UIColor
redColor];
//将UIImageView加入到父视图
[self.view
addSubview:self.imgV];
UIButton *btn1 = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(180,
320, 100,
44);
[btn1 setTitle:@"异步下载" forState:UIControlStateNormal];
[btn1 addTarget:self
action:@selector(sendAsyn1:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:btn1];
}
double length =
0.00;//用于存放数据的总长度
//获得服务器响应后调用该方法,只调用一次,可以获取响应对象,而进一步通过响应对象得知响应的期望长度
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"建立数据连接");
self.data = [[NSMutableData
alloc]
init];//为NSMutableData开辟空间
length = response.expectedContentLength;//获取数据的总长度
}
//该方法是接受到响应后调用该方法,但是在整个请求过程中会调用多次,data参数就是每次获取的数据块
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.data
appendData:data];//反复执行该段语句 拼接数据块 直到完成接收
double dataSource =(self.data.length/length)*100
;//当前获得的数据/数据总长度 就是它的百分比
NSLog(@"数据接收到%.2f%%", dataSource );
self.imgV.image = [UIImage
imageWithData:self.data];//显示已经载好的数据直到完成
}
//整个连接请求完成,数据加载完毕之后调用方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"接收完成");
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
NSURLConnection需遵循NSURLConnectionDataDelegate协议,协议必须得实现方法包括:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
#import "ViewController.h"
#define URL02 @"http://ww3.sinaimg.cn/bmiddle/c8dc68bajw1eaw9bpj6kfj20by0bymyw.jpg"
@interface
ViewController ()
@end
@implementation ViewController
- (void)sendAsyn1:(NSString *)urlStr
{
NSString *path =
URL02;
NSURL *url = [NSURL
URLWithString:path];
NSURLRequest *request = [[NSURLRequest
alloc] initWithURL:url];
[NSURLConnection
connectionWithRequest:request delegate:self];
NSLog(@"发起异步连接");
}
- (void)viewDidLoad
{
[super
viewDidLoad];
//创建一个UIImageView框架用于放图片
self.imgV = [[UIImageView
alloc] initWithFrame:CGRectMake(0,
0, 320,
300)];
//测试是否创建成功 设置它的颜色为红色
self.imgV.backgroundColor = [UIColor
redColor];
//将UIImageView加入到父视图
[self.view
addSubview:self.imgV];
UIButton *btn1 = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(180,
320, 100,
44);
[btn1 setTitle:@"异步下载" forState:UIControlStateNormal];
[btn1 addTarget:self
action:@selector(sendAsyn1:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:btn1];
}
double length =
0.00;//用于存放数据的总长度
//获得服务器响应后调用该方法,只调用一次,可以获取响应对象,而进一步通过响应对象得知响应的期望长度
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"建立数据连接");
self.data = [[NSMutableData
alloc]
init];//为NSMutableData开辟空间
length = response.expectedContentLength;//获取数据的总长度
}
//该方法是接受到响应后调用该方法,但是在整个请求过程中会调用多次,data参数就是每次获取的数据块
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.data
appendData:data];//反复执行该段语句 拼接数据块 直到完成接收
double dataSource =(self.data.length/length)*100
;//当前获得的数据/数据总长度 就是它的百分比
NSLog(@"数据接收到%.2f%%", dataSource );
self.imgV.image = [UIImage
imageWithData:self.data];//显示已经载好的数据直到完成
}
//整个连接请求完成,数据加载完毕之后调用方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"接收完成");
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄
- 74 NSURLConnection的基本使用
- iOS开发之网络编程--使用NSURLConnection实现文件上传
- iPhone开发使用NSURLConnection 的小实例
- iOS开发网络篇—NSURLConnection基本使用
- iOS开发网络篇—NSURLConnection基本使用(一)
- iOS中使用NSURLConnection处理HTTP同步与异步请求
- 关于使用NSURLConnection发送get请求响应为nil的情况
- iPhone网络开发之如何使用NSURLConnection
- 网络接口 使用NSURLConnection完成Get和Post方法
- iOS开发网络篇—NSURLConnection基本使用
- 使用NSMutableURLRequest和NSURLConnection 以Post方式上传照片
- IOS开发--网络篇-->NSURLConnection基本使用
- NSURLConnection 的使用
- xcode 4.6 使用NSURLConnection 获取网页内容(iOS6.1,纯手工编码,无xib,无storyboard)
- iOS开发之网络编程--使用NSURLConnection实现大文件下载
- 75 NSURLConnection的基本使用示例
- NSURLConnection使用详解
- NSURLConnection经常使用的代理方法
- iOS开发网络篇—NSURLConnection基本使用(二)