首先
在声明一个全局的
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{
}
本文介绍了如何利用NSURLConnection对象发起HTTP请求,并详细解释了如何实现请求过程中的各种回调方法,如接收响应、处理数据及错误等。
3745

被折叠的 条评论
为什么被折叠?



