UI 网络请求协议 NSURLConnectionDataDelegate
2015-08-12 11:22
561 查看
// 将容器初始化
_webData =[[NSMutableDataalloc]initWithCapacity:0];
// 请求体
NSURLRequest*request=[[NSURLRequestalloc]initWithURL:[NSURLURLWithString:kImageURLString]];
_connection =[[NSURLConnectionalloc]initWithRequest:request
delegate:self];
#pragma mark-NSURLConnectionDataDelegate
//链接失败的回调
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError
*)error
{
NSLog(@"链接发起失败%@",error);
}
//数据返回的回调
//当请求的数据总量非常大的时候,服务器会根据协议将数据压缩成数个小单元
//这个方法只要接收到小单元就会出现回调,所以这个方法可能会在一次请求调用多次
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData
*)data
{
NSLog(@"返回的数据%@",data);
}
//收到响应
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse
*)response
{
NSLog(@"response Header,%@",[(NSHTTPURLResponse
*) response allHeaderFields]);
NSLog(@"response status %ld",[(NSHTTPURLResponse*)
response statusCode]);
}
//请求完成
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"请求完成!");
// NSError *error=nil;
/*
NSJSONSerialization 原始数据
NSJSONReadingMutbleContainers 转化成容器
*/
// id object=[NSJSONSerialization JSONObjectWithData:_webData options:NSJSONReadingMutableContainers error:&error];
// NSLog(@"%@ %@",[object class],object);
UIImageView *image=[[UIImageViewalloc]initWithFrame:CGRectZero];
image.center=self.view.center;
image.image=[UIImageimageWithData:_webData];
[self.viewaddSubview:image];
[UIViewanimateWithDuration:2animations:^{
image.frame=self.view.bounds;
[image setYOffset:64];
}];
[image release];
}
_webData =[[NSMutableDataalloc]initWithCapacity:0];
// 请求体
NSURLRequest*request=[[NSURLRequestalloc]initWithURL:[NSURLURLWithString:kImageURLString]];
_connection =[[NSURLConnectionalloc]initWithRequest:request
delegate:self];
#pragma mark-NSURLConnectionDataDelegate
//链接失败的回调
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError
*)error
{
NSLog(@"链接发起失败%@",error);
}
//数据返回的回调
//当请求的数据总量非常大的时候,服务器会根据协议将数据压缩成数个小单元
//这个方法只要接收到小单元就会出现回调,所以这个方法可能会在一次请求调用多次
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData
*)data
{
NSLog(@"返回的数据%@",data);
}
//收到响应
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse
*)response
{
NSLog(@"response Header,%@",[(NSHTTPURLResponse
*) response allHeaderFields]);
NSLog(@"response status %ld",[(NSHTTPURLResponse*)
response statusCode]);
}
//请求完成
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"请求完成!");
// NSError *error=nil;
/*
NSJSONSerialization 原始数据
NSJSONReadingMutbleContainers 转化成容器
*/
// id object=[NSJSONSerialization JSONObjectWithData:_webData options:NSJSONReadingMutableContainers error:&error];
// NSLog(@"%@ %@",[object class],object);
UIImageView *image=[[UIImageViewalloc]initWithFrame:CGRectZero];
image.center=self.view.center;
image.image=[UIImageimageWithData:_webData];
[self.viewaddSubview:image];
[UIViewanimateWithDuration:2animations:^{
image.frame=self.view.bounds;
[image setYOffset:64];
}];
[image release];
}
相关文章推荐
- 关于HttpClient在服务器端使用请求碰到的问题总结
- 网络图片下载到手机返回null的问题解决方法
- Http响应码
- Java安全通信:HTTPS与SSL
- HTTP状态码
- TCP长连接与短连接的区别
- HTTP、TCP、UDP、Socket基本概念
- Poco C++——HTTP的post请求和get请求
- TCP/IP格式
- IM开发通信协议基础知识(一)---TCP、UDP、HTTP、SOCKET
- TCP/IP协议头部结构体(网摘小结)(转)
- 计算机网络的五层协议体系
- 如何计算网络地址和广播地址
- SSG140 web、telnet、ssh无法访问,仅支持console
- 网络篇----http协议
- iOS 对基于AFNetworking框架网络请求的简单封装
- android之ConnectivityManager简介,网络连接状态
- BIM360: C#如何发送HTTP GET和POST请求登陆BIM 360 Glue以及获取项目列表
- TQ2440 学习笔记—— 2、Win7PC 与虚拟机Ubuntu互通 ping 的网络设置
- 虚拟机中如何进行正确的网络配置