原文地址:IPhpne之获取远程网页数据作者:飞舞的鸡毛
Load函数:
{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self];
[connection release];
[request release];
}
当创建完NSURL_Request 和NSURLConnection 之后直接就release了.其原因是当有什么事件需要处理时,对象就自动被唤醒.
// 回调方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog (@"connectionDidReceiveResponse");
}
//此方法表名 提供一个NSDATA,它封装了刚从连接上收到的字节块
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data {
NSLog (@"connectionDidReceiveData");
NSString *newText = [[NSString alloc]
initWithData:data
encoding:NSUTF8StringEncoding];
if (newText != NULL) {
[self appendTextToView:newText];
[newText release];
}
}
//下载已经完成
- (void) connectionDidFinishLoading: (NSURLConnection*) connection {
[activityIndicator stopAnimating];
}
//下载失败 提供一个提供一个失败的原因
-(void) connection:(NSURLConnection *)connection
didFailWithError: (NSError *)error {
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle: [error localizedDescription]
message: [error localizedFailureReason]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
[activityIndicator stopAnimating];
}
留言列表