#import "ReWeather.h"
#import "DayWeather.h"
@interface ReWeather(private)
- (NSUInteger)calculateWeekNum:(NSString*)dateStr;
- (void)splitXmlWeatherInfo;
- (void)outputParseInfo;
- (void)outputDayWeatherInfo;
@end
//#define kWeatherServiceURLStr @"http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName="
@implementation ReWeather
@synthesize dayWeather;
@synthesize weekArray;
-(id)init
{
if (self = [super init])
{
weekArray = [[NSMutableArray alloc] init];
dayWeather = [[DayWeather alloc] init];
}
return self;
}
-(void)dealloc
{
[super dealloc];
[weekArray release];
[dayWeather release];
}
-(void)updateWeatherInfo:(NSString *)name
{
NSString *weatherRequestUrlStr = [NSString stringWithFormat:
@"http://www.google.com/ig/api?hl=zh-cn&weather=%@",
[name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//NSString *weatherRequestUrlStr = [NSString stringWithFormat:@"%@%@",kWeatherServiceURLStr,[[ipCityLocation citySimpleName]
// stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"request = %@", weatherRequestUrlStr);
NSData *weatherReponseData = [NSData dataWithContentsOfURL:[NSURL URLWithString:weatherRequestUrlStr]];
//NSLog(@"data = %@", weatherReponseData);
//转换数据的格式,转为utf-8的格式以显示中文(汉字占3个字符)
CFStringRef gb2312Str = CFStringCreateWithBytes(NULL,
[weatherReponseData bytes],
[weatherReponseData length],
kCFStringEncodingGB_18030_2000, FALSE);
NSLog(@"0000000000000create CFStringRef %@", gb2312Str);
if (nil == gb2312Str)
{
NSLog(@"0000000000000create CFStringRef fail");
}
else
{
NSLog(@"0000000000000create CFStringRef succes");
NSString *gb2312NSString = (NSString *)gb2312Str;
NSLog(@"0000000000000create CFStringRef %@", gb2312NSString);
weatherReponseData = [gb2312NSString dataUsingEncoding:NSUTF8StringEncoding];
}
xmlParser = [[NSXMLParser alloc] initWithData:weatherReponseData];
NSLog(@"%@", xmlParser);
[xmlParser setDelegate:self];
[xmlParser setShouldProcessNamespaces:NO];
[xmlParser setShouldReportNamespacePrefixes:NO];
[xmlParser setShouldResolveExternalEntities:NO];
[xmlParser parse];
NSLog(@"2222222222");
[xmlParser release];
//CFRelease(gb2312Str);
}
/*
* 目前依旧采用jy的方法
* 问题:晚上时,显示时间是明天 后天 大后天,这样取的时间不准
*/
#pragma mark xml parser delegate
- (void)parserDidEndDocument:(NSXMLParser *)parser{
// [self outputParseInfo];
//
// //取出xml中数据后提取信息
// [self splitXmlWeatherInfo];
NSLog(@".......%@", weekArray);
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
// NSLog(@"1111111111111111");
// NSLog(@"name = %@", elementName);
if ([elementName isEqualToString:@"current_conditions"])
{
[weekArray addObject:dayWeather];
[dayWeather release];
//dayWeather = nil;
dayWeather = [[DayWeather alloc] init];
}
if ([elementName isEqualToString:@"forecast_conditions"])
{
[weekArray addObject:dayWeather];
[dayWeather release];
//dayWeather = nil;
dayWeather = [[DayWeather alloc] init];
}
if ([elementName isEqualToString:@"weather"])
{
//[dayWeather release];
// dayWeather = nil;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"value = %@", string);
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if (qName)
{
elementName = qName;
}
if ( [elementName isEqualToString:@"city"])
{
NSLog(@"City is %@", [attributeDict objectForKey:@"data"]);
dayWeather.cityStr = [attributeDict objectForKey:@"data"];
}
if ( [elementName isEqualToString:@"condition"])
{
NSLog(@"conditions is %@", [attributeDict objectForKey:@"data"]);
dayWeather.conditions = [attributeDict objectForKey:@"data"];
}
if ( [elementName isEqualToString:@"forecast_date"])
{
NSLog(@"forecast_date is %@", [attributeDict objectForKey:@"data"]);
dayWeather.dateStr = [attributeDict objectForKey:@"data"];
}
if ( [elementName isEqualToString:@"day_of_week"])
{
NSLog(@"day_of_week is %@", [attributeDict objectForKey:@"data"]);
dayWeather.weekStr = [attributeDict objectForKey:@"data"];
}
if ( [elementName isEqualToString:@"temp_f"])
{
NSLog(@"tempF is %@", [attributeDict objectForKey:@"data"]);
dayWeather.tempF = [attributeDict objectForKey:@"data"];
}
if ( [elementName isEqualToString:@"temp_c"])
{
NSLog(@"tempC is %@", [attributeDict objectForKey:@"data"]);
dayWeather.tempC = [attributeDict objectForKey:@"data"];
}
if ( [elementName isEqualToString:@"humidity"])
{
NSLog(@"humidity is %@", [attributeDict objectForKey:@"data"]);
dayWeather.humidity = [attributeDict objectForKey:@"data"];
}
if ( [elementName isEqualToString:@"low"])
{
NSLog(@"low is %@", [attributeDict objectForKey:@"data"]);
dayWeather.lowStr = [attributeDict objectForKey:@"data"];
}
if ( [elementName isEqualToString:@"high"])
{
NSLog(@"high is %@", [attributeDict objectForKey:@"data"]);
dayWeather.highStr = [attributeDict objectForKey:@"data"];
}
if ( [elementName isEqualToString:@"icon"])
{
NSLog(@"icon is %@", [attributeDict objectForKey:@"data"]);
dayWeather.iconStr = [attributeDict objectForKey:@"data"];
}
if ( [elementName isEqualToString:@"wind_condition"])
{
NSLog(@"wind_condition is %@", [attributeDict objectForKey:@"data"]);
dayWeather.windInfo = [attributeDict objectForKey:@"data"];
}
}
/*
*蔡勒公式
*W=[C/4]-2C+y+[y/4]+[26(m+1)/10]+d-1
*其中,W是所求日期的星期数.如果求得的数大于7,可以减去7的倍数,
*直到余数小于7为止.c是公元年份的前两位数字,y是以知公元年份的
*后两位数字;m是月数,d是日数.方括[ ]表示只截取该数的整数部分.
*还有一个特别要注意的地方:所求的月份如果是1月或2月,则应视为
*前一年的13月或14月.所以公式中m 的取值范围不是1-12,而是3-14.
*/
- (NSUInteger)calculateWeekNum:(NSString*)dateStr{
NSArray *separateStrArray = [[[NSArray alloc] initWithArray:[dateStr componentsSeparatedByString:@"-"]] autorelease];
NSString *year = [separateStrArray objectAtIndex:0];
NSString *month = [separateStrArray objectAtIndex:1];
NSString *idate = [separateStrArray objectAtIndex:2];
//防止字符串里面有空格,先全部转换成整数,再求余或者mod
NSInteger Year = [year integerValue];
NSInteger Month = [month integerValue];
NSInteger Day = [idate integerValue];
NSInteger weekNum;
//对一月、二月特殊处理,当作前一年的13、14月
if (Month == 1 || Month == 2) {
Month += 12;
Year--;
}
NSInteger CYear = Year/100;
NSInteger Byear = Year%100;
weekNum = (CYear/4) - 2*CYear + Byear + (Byear/4) + (13 * (Month + 1)/5) + Day - 1;
if (weekNum <= 0)
{
weekNum = weekNum + 70;
}
weekNum = weekNum % 7;
return weekNum;
}
@end
方法二
#import "TouchXML.h"
@implementation GetData
- (NSMutableArray *) getWeatherData:(NSString *)query
{
NSMutableArray *others = [[NSMutableArray alloc] init ];
CXMLDocument *parser = [[[CXMLDocument alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/ig/api?hl=zh-&weather=%@", query]] options:0 error:nil] autorelease];
NSLog(@"abc%d", [[[[[parser rootElement] children]objectAtIndex:0] children] count] );
NSArray *weathers = [[[[parser rootElement] children]objectAtIndex:0] children];
NSLog(@"aa%d",[weathers count]);
for (CXMLElement *element in weathers)
{
if ([element isKindOfClass:[CXMLElement class]])
{
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
for (int i = 0; i < [element childCount]; i++)
{
if ([[[element children] objectAtIndex:i] isKindOfClass:[CXMLElement class]])
{
NSLog(@"element%@",[element childAtIndex:i]);
[item setObject:[[[element childAtIndex:i] attributeForName:@"data"] stringValue]
forKey:[[element childAtIndex:i] name]
];
}
}
[others addObject:item];
//[others release];
}
}
NSLog(@"dd%@", others);
return others;
}
- (TodayWeatherModel *)getTodayData:(NSString*)query
{
NSMutableArray * weatherData = [self getWeatherData:query];
TodayWeatherModel * todayData = [[[TodayWeatherModel alloc]init] autorelease];
todayData.city = [[weatherData objectAtIndex:0] objectForKey:@"city"];
todayData.todayDate = [[weatherData objectAtIndex:0] objectForKey:@"forecast_date"];
todayData.conditionStatus = [[weatherData objectAtIndex:1] objectForKey:@"condition"];
todayData.ctemp = [[weatherData objectAtIndex:1] objectForKey:@"temp_c"];
todayData.humidity = [[[weatherData objectAtIndex:1] objectForKey:@"humidity"] substringFromIndex:10];
todayData.imageUrl = [[weatherData objectAtIndex:1] objectForKey:@"icon"];
todayData.windDirect = [[[weatherData objectAtIndex:1] objectForKey:@"wind_condition"] substringFromIndex:5];
return todayData;
}