WEB开发网
开发学院手机开发iPhone 开发 iOS开发笔记:网络编程总结 阅读

iOS开发笔记:网络编程总结

 2010-10-13 02:04:00 来源:本站整理   
核心提示:*)error;- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;- (void)connectionDidFinishLoading:(NSURLConnection *)connection;3. 实现委托方
*)error;

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

3. 实现委托方法

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

// store data

[self.receivedData setLength:0]; //通常在这里先清空接受数据的缓存

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

/* appends the new data to the received data */

[self.receivedData appendData:data]; //可能多次收到数据,把新的数据添加在现有数据最后

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

// 错误处理

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

// disconnect

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

NSString *returnString = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];

NSLog(returnString);

[self urlLoaded:[self urlString] data:self.receivedData];

firstTimeDownloaded = YES;

}

三:使用NSXMLParser解析xml文件

1. 设置委托对象,开始解析

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; //或者也可以使用initWithContentsOfURL直接下载文件,但是有一个原因不这么做:

// It's also possible to have NSXMLParser download the data, by passing it a URL, but this is not desirable

// because it gives less control over the network, particularly in responding to connection errors.

[parser setDelegate:self];

[parser parse];

2. 常用的委托方法

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName

namespaceURI:(NSString *)namespaceURI

上一页  1 2 3 4 5  下一页

Tags:iOS 开发 笔记

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接