NSURLConnection请求的,基本实现方法。

首先

 

在声明一个全局的

 

NSURLConnection 对象,

然后在制定的加载方法里面实现请求,或者写一个测试方法或者类方法去实现。

 

    NSString * str = @"http://woainike.iteye.com";
    
    NSURL * nsURL = [NSURL URLWithString:str];
    
    NSURLRequest * urlRequest = [NSURLRequest requestWithURL:nsURL];
    
    NSURLConnection * urlConnecction = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
    //为了安全,捕捉不存在的连接
    if(urlConnecction != nil)
        return;

 

 

#pragma mark -
#pragma mark NSURLConnectionDataDelegate methods

// The following are delegate methods for NSURLConnection. Similar to callback functions, this is how
// the connection object,  which is working in the background, can asynchronously communicate back to
// its delegate on the thread from which it was started - in this case, the main thread.
//

/**
 *  
 *实现必要的方法
 **/


// -------------------------------------------------------------------------------
//	connection:didReceiveResponse:response 通过response的响应,判断是否连接存在
// -------------------------------------------------------------------------------
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    
}


// -------------------------------------------------------------------------------
//	connection:didReceiveData:data,通过data获得请求后,返回的数据,数据类型NSData
// -------------------------------------------------------------------------------
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    
    
    
    
}
// -------------------------------------------------------------------------------
//	connection:didFailWithError:error 返回的错误信息
// -------------------------------------------------------------------------------

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    
}
// -------------------------------------------------------------------------------
//	connectionDidFinishLoading:connection 数据请求完毕,这个时候,用法是多线程的时候,通过这个通知,关部子线程
// -------------------------------------------------------------------------------
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值