iOS开发笔记:网络编程总结
2010-10-13 02:04:00 来源:本站整理- (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
- ››开发学院教你用SQL 语句最快速清空MySQL 数据表的...
- ››iOS Safari支持浏览器内方向感应
- ››ios实现条形码扫描功能
- ››iOS开发知识:Core data操作含有初始数据的sqlite...
- ››iOS iPhone官方参考资料明细
- ››iOS开发基础:Modal View Controller的不同呈现方...
- ››iOS如何处理设备方向变化
- ››iOS开发基础之@property关键字
- ››iOS4下实现UIView动画结束后调用事件的新方法
- ››iOS开发代码:从文本文件中读取内容到字符串里
- ››iOS开发基础-UITableView
- ››iOS 开发者应该知道的 ARM 结构
赞助商链接