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
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