Gavin.han

致力于移动开发 技术改变生活
随笔 - 133, 文章 - 0, 评论 - 46, 阅读 - 42万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

ios开发 简易的天气应用(GET、XML)

Posted on   gavin.han  阅读(1529)  评论(0编辑  收藏  举报

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
复制代码

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示