1.新建Empty Application,添加UIViewController视图,视图设计如下:
// HomeViewController.h
// WeatherQuery
#import <UIKit/UIKit.h>
@interface HomeViewController : UIViewController<UIApplicationDelegate, UISearchBarDelegate>
{
NSMutableData *data;//用于接收服务器返回的结果
NSMutableArray *Elements;//对服务器返回结果处理后所有XML元素里面的文字
NSString *Element;//解析XML某个元素时存储其中的文字,这个元素解析完后Element后,被加到Elements中
}
@property (retain, nonatomic) IBOutlet UILabel *city;
@property (retain, nonatomic) IBOutlet UILabel *date;
@property (retain, nonatomic) IBOutlet UITextView *detail;
@end
#import <UIKit/UIKit.h>
@interface HomeViewController : UIViewController<UIApplicationDelegate, UISearchBarDelegate>
{
NSMutableData *data;//用于接收服务器返回的结果
NSMutableArray *Elements;//对服务器返回结果处理后所有XML元素里面的文字
NSString *Element;//解析XML某个元素时存储其中的文字,这个元素解析完后Element后,被加到Elements中
}
@property (retain, nonatomic) IBOutlet UILabel *city;
@property (retain, nonatomic) IBOutlet UILabel *date;
@property (retain, nonatomic) IBOutlet UITextView *detail;
@end
API:
思路:获取数据,接收数据,解析数据,显示数据
// HomeViewController.m
// WeatherQuery
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
@synthesize city;
@synthesize date;
@synthesize detail;
//UISearchBarDelegate协议的响应事件
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
NSString *search = searchBar.text;
NSString *urlString = [@"http://www.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?theUserID=xxxxxxxx&theCityCode=" stringByAppendingString:search];
//在生成NSURL对象时,需要调用NSString的stringByAppendingString
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];
data = [[NSMutableData data] retain];
[NSURLConnection connectionWithRequest:request delegate:self];
}
//收到响应时,将data长度初始化为0
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[data setLength:0];
}
//当接收到数据后,把数据加到data后面
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)incomingData
{
[data appendData:incomingData];
}
//数据传送完毕,使用NSXMLParser对数据进行解析
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
Elements = [NSMutableArray arrayWithCapacity:0];
[parser parse];
[parser release];
[data release];
}
//连接失败,输出日志并释放data对象
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"天气查询出错:%@", [error localizedDescription]);
[data release];
}
//开始对一个XML元素进行解析,将Element初始化为空字符串
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
Element = @"";
}
//对一个XML元素解析完成,将Element加到Elements数组中
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
[Elements addObject:Element];
}
//对元素解析过程中,将得到的字符串存加到Element后面。因为这里只是得到元素的部分字符,所以对元素解析过程中会多次
//调用这个方法,将所有的连起来才是完整的字符串
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
Element = [Element stringByAppendingString:string];
}
//对XML文档解析完毕,将相关信息显示到界面中
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
city.text = [Elements objectAtIndex:0];
date.text = [Elements objectAtIndex:3];
detail.text = [[[Elements objectAtIndex:4] stringByAppendingString:@"\n"] stringByAppendingString:[Elements objectAtIndex:6]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[self setCity:nil];
[self setDate:nil];
[self setDetail:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[city release];
[date release];
[detail release];
[super dealloc];
}
@end
// WeatherQuery
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
@synthesize city;
@synthesize date;
@synthesize detail;
//UISearchBarDelegate协议的响应事件
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
NSString *search = searchBar.text;
NSString *urlString = [@"http://www.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?theUserID=
//在生成NSURL对象时,需要调用NSString的stringByAppendingString
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];
data = [[NSMutableData data] retain];
[NSURLConnection connectionWithRequest:request delegate:self];
}
//收到响应时,将data长度初始化为0
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[data setLength:0];
}
//当接收到数据后,把数据加到data后面
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)incomingData
{
[data appendData:incomingData];
}
//数据传送完毕,使用NSXMLParser对数据进行解析
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
Elements = [NSMutableArray arrayWithCapacity:0];
[parser parse];
[parser release];
[data release];
}
//连接失败,输出日志并释放data对象
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"天气查询出错:%@", [error localizedDescription]);
[data release];
}
//开始对一个XML元素进行解析,将Element初始化为空字符串
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
Element = @"";
}
//对一个XML元素解析完成,将Element加到Elements数组中
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
[Elements addObject:Element];
}
//对元素解析过程中,将得到的字符串存加到Element后面。因为这里只是得到元素的部分字符,所以对元素解析过程中会多次
//调用这个方法,将所有的连起来才是完整的字符串
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
Element = [Element stringByAppendingString:string];
}
//对XML文档解析完毕,将相关信息显示到界面中
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
city.text = [Elements objectAtIndex:0];
date.text = [Elements objectAtIndex:3];
detail.text = [[[Elements objectAtIndex:4] stringByAppendingString:@"\n"] stringByAppendingString:[Elements objectAtIndex:6]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[self setCity:nil];
[self setDate:nil];
[self setDetail:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[city release];
[date release];
[detail release];
[super dealloc];
}
@end
致力于ios开发
分类:
ios小案例
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架