//
// WeatherReportViewController.m
// WeatherReport
//
// Created by a a on 10-7-22.
// Copyright a 2010. All rights reserved.
//
//#import "WeatherLoadViewController.h"
#import "WeatherReportViewController.h"
static NSString *const urlAddr = @"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; //连接地址
@implementation WeatherReportViewController
@synthesize city;
@synthesize hlTemperature;
@synthesize weather;
@synthesize windyDirection;
@synthesize tempString;
@synthesize weatherImageView1;
@synthesize weatherImageView2;
@synthesize currentWeather;
@synthesize timeLabel;
@synthesize tomorrowWeather;
@synthesize tomorrowTemperature;
@synthesize tomorrowWindy;
@synthesize tomorrowImage1;
@synthesize tomorrowImage2;
@synthesize afterTomorrowWeather;
@synthesize afterTomorrowtemperature;
@synthesize afterTomorrowWindy;
@synthesize afterTomorrowImage1;
@synthesize afterTomorrowImage2;
@synthesize weatherQueryField;
@synthesize downloadView;
@synthesize activityindicator;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[activityindicator startAnimating];
NSLog(@"updateWeather");
[self connectionWebService];
}
//连接服务器
- (void) connectionWebService {
weatherData = [[NSMutableData alloc] init];
NSMutableString *soapMessage =[[NSMutableString alloc] init];
soapMessage = [NSMutableString stringWithFormat:
@"<?xml version=/"1.0/" encoding=/"utf-8/"?>/n"
"<soap:Envelope xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/" xmlns:soap=/"http://schemas.xmlsoap.org/soap/envelope//">/n"
"<soap:Body>/n"
"<getWeatherbyCityName xmlns=/"http://WebXml.com.cn//">/n"
"<theCityName>%@</theCityName>/n"
"</getWeatherbyCityName>/n"
"</soap:Body>/n"
"</soap:Envelope>/n",self.queryWeather];
//建立连接
NSURL *url = [NSURL URLWithString:urlAddr];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *soapmLength = [NSString stringWithFormat: @"%d", [soapMessage length]];
//post方式提交数据
[theRequest addValue: @"text/xml;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://WebXml.com.cn/getWeatherbyCityName" forHTTPHeaderField:@"SOAPACtion"];
[theRequest addValue: soapmLength forHTTPHeaderField: @"Content-Length"];
[theRequest setHTTPMethod: @"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
//[soapMessage release];
//NSURLConnection于服务器交互
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest: theRequest
delegate:self];
[connection release];
NSLog(@"%d", [weatherData retainCount]);
NSLog(@"%d", [soapMessage retainCount]);
}
//给NSXMLParser赋值函数,开始解析xml
- (void) startParsingWeather {
NSLog(@"startParsingWeather");
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:weatherData];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:YES];
[parser setShouldResolveExternalEntities: YES];
NSError *parseError = [parser parserError] ;
//parseError = [parser parserError];
if (parseError) {
NSLog(@"parser parserError");
}
[parser parse];
[parser release];
}
#pragma mark NSURLConnection
//收到服务器回答
- (void) connection:(NSURLConnection*) conneciton
didReceiveResponse:(NSURLResponse*) response {
NSLog(@"connection:didReceiveResponse:");
}
//接收数据
- (void) connection: (NSURLConnection*) conneciton
didReceiveData: (NSData*) data {
NSLog(@"connection:didReceiveData:");
[weatherData appendData: data];
}
//完成数据下载
- (void) connectionDidFinishLoading: (NSURLConnection*) conneciton {
NSLog(@"connectionDidFinishLoading:");
[self startParsingWeather];
}
//错误处理
- (void) conneciton: (NSURLConnection*) conneciton
didFailWithError:(NSError*) error {
NSLog(@"conneciton:didFailWithError:");
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:[error localizedDescription]
message:[error localizedFailureReason]
delegate:nil cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
#pragma mark NSXMLParser
//初始化数组
- (void) parserDidStartDocument: (NSXMLParser*) parser {
NSLog(@"parserDidStartDocument");
weatherArray = [[NSMutableArray alloc] init];
}
//纪录元素开始标签,并出始化临时数组
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"string"]) {
if(!tempString) {
tempString = [[NSMutableString alloc] init];
}
}
}
//提取XML标签中的元素,反复取多次
- (void) parser: (NSXMLParser*) parser
foundCharacters:(NSString*) string {
NSLog(@"parser: foundCharacters:");
if(tempString) {
[tempString appendString:string];
}
}
//纪录元素结尾标签,并把临时数组中的元素存到字符串数组中
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"string"])
{
[weatherArray addObject: tempString];
[tempString release];
tempString = nil;
}
}
//结束分析,调用显示函数
- (void) parserDidEndDocument: (NSXMLParser*) parser {
NSLog(@" parserDidEndDocument:");
NSLog(@"%@",[weatherArray objectAtIndex:0]);
[self displayView];
}
#pragma mark displayView
//显示界面函数
- (void) displayView {
[activityindicator stopAnimating];
city.text = [weatherArray objectAtIndex:1];
hlTemperature.text = [weatherArray objectAtIndex:5];
weather.text = [weatherArray objectAtIndex:6];
windyDirection.text = [weatherArray objectAtIndex:7];
weatherImageView1.image = [UIImage imageNamed:[NSMutableString stringWithFormat:@"b_%@",[weatherArray objectAtIndex:8]]];
weatherImageView2.image = [UIImage imageNamed:[NSMutableString stringWithFormat:@"b_%@",[weatherArray objectAtIndex:9]]];
currentWeather.font = [UIFont fontWithName: @"Arial" size: 14.0];
currentWeather.lineBreakMode = UILineBreakModeWordWrap;
currentWeather.numberOfLines = 0;
currentWeather.text = [weatherArray objectAtIndex:10];
tomorrowTemperature.text = [weatherArray objectAtIndex:12];
tomorrowWeather.text = [weatherArray objectAtIndex:13];
tomorrowWindy.text = [weatherArray objectAtIndex:14];
tomorrowImage1.image = [UIImage imageNamed:[weatherArray objectAtIndex:15]];
tomorrowImage2.image = [UIImage imageNamed:[weatherArray objectAtIndex:16]];
afterTomorrowtemperature.text = [weatherArray objectAtIndex:17];
afterTomorrowWeather.text = [weatherArray objectAtIndex:18];
afterTomorrowWindy.text = [weatherArray objectAtIndex:19];
afterTomorrowImage1.image = [UIImage imageNamed:[weatherArray objectAtIndex:20]];
afterTomorrowImage2.image = [UIImage imageNamed:[weatherArray objectAtIndex:21]];
timeLabel.text = [weatherArray objectAtIndex:4];
downloadView.hidden = YES;
}
#pragma mark Query
//返回查询城市代码字符串
- (NSString*) queryWeather {
return weatherQueryField.text;
}
//查询按钮处理函数,包括关闭键盘
- (IBAction) queryBegain {
downloadView.hidden = NO;
[weatherQueryField resignFirstResponder];
[self connectionWebService];
}
//按Return键,关闭键盘,在头文件里定义UITextFieldDelegate委托,
//并把文件输入编辑框委托给file's owner
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textField == weatherQueryField)
{
[self queryBegain];
}
return YES;
}
#pragma mark Finish
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
//[soapMessage dealloc];
}
@end
这是一个使用Objective-C编写的iPhone天气预报应用示例,通过连接服务器获取数据,使用SOAP请求来查询指定城市的天气信息,包括当前及未来几天的天气、温度、风向等,并在界面上展示。
522

被折叠的 条评论
为什么被折叠?



