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


    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个数,计算出正数和负数的个数。
    浏览器允许的并发请求资源数 优化
    strict 严格模式
    AMD 和 CMD 的区别
    Canvas
    visual filters 滤镜 ie
    ie 如何判断正在执行的脚本
    async
    富文本编辑器
    检测CSS属性 是否支持
  • 原文地址:https://www.cnblogs.com/hanjun/p/2781572.html
Copyright © 2020-2023  润新知