Wrapper 클래스 사용시
http://theeye.pe.kr/entry/http-wrapping-class-with-iphone-network-programming
[헤더파일]
// Google Weather Service
NSMutableData *responseData; //서버로부터 받은 자료를 담는 변수
NSURL *theURL;
// Information
NSString *location;
NSString *date;
// Current Conditions
UIImage *icon;
NSString *temp;
NSString *humidity;
NSString *wind;
NSString *condition;
// Forecast Conditions
NSMutableArray *days;
NSMutableArray *icons;
NSMutableArray *temps;
NSMutableArray *conditions;
[소스파일]
- (void)queryService:(NSString *)city withParent:(UIViewController *)controller {
viewController = (MainViewController *)controller;
responseData = [[NSMutableData data] retain];
NSString *url = [NSString stringWithFormat:@"http://www.google.com/ig/api?weather=%@", city]; //서버url을 적는다.
theURL = [[NSURL URLWithString:url] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:theURL];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse {
[theURL autorelease];
theURL = [[request URL] retain];
return request;
}
// 서버로 부터 답변이 처음 오면 호출된다. 자료는 없다.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
// 서버로 부터 데이터를 받으면서 계속 호출된다. responseData에 계속 추가한다.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
// 에러가 발생하면 호출된다.
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Handle Error
}
// 모든 응답이 오고나서 연결이 해제되면 호출된다.
// responseData에 있는 자료를 사용하면 된다.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//NSString *content = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];
//NSLog( @"Data = %@", content );
days = [[NSMutableArray alloc] init];
icons = [[NSMutableArray alloc] init];
temps = [[NSMutableArray alloc] init];
conditions = [[NSMutableArray alloc] init];
NSString *xpathQueryString;
NSArray *nodes;
// Forecast Information
// NSString *location;
xpathQueryString = @"//forecast_information/city/@data";
nodes = PerformXMLXPathQuery(responseData, xpathQueryString);
for ( NSDictionary *node in nodes ) {
for ( id key in node ) {
if( [key isEqualToString:@"nodeContent"] ) {
location = [node objectForKey:key];
}
}
}
NSLog(@"location = %@", location);
// NSString *date;
xpathQueryString = @"//forecast_information/forecast_date/@data";
nodes = PerformXMLXPathQuery(responseData, xpathQueryString);
for ( NSDictionary *node in nodes ) {
for ( id key in node ) {
if( [key isEqualToString:@"nodeContent"] ) {
date = [node objectForKey:key];
}
}
}
NSLog(@"date = %@", date);
// Current Conditions
// UIImage *icon;
xpathQueryString = @"//current_conditions/icon/@data";
nodes = PerformXMLXPathQuery(responseData, xpathQueryString);
for ( NSDictionary *node in nodes ) {
for ( id key in node ) {
if( [key isEqualToString:@"nodeContent"] ) {
icon = [NSString stringWithFormat:@"http://www.google.com%@", [node objectForKey:key]];
}
}
}
NSLog(@"icon = %@", icon);
// NSString *temp
NSString *temp_f;
NSString *temp_c;
xpathQueryString = @"//current_conditions/temp_f/@data";
nodes = PerformXMLXPathQuery(responseData, xpathQueryString);
//NSLog( @"nodes = %@", nodes );
for ( NSDictionary *node in nodes ) {
for ( id key in node ) {
if( [key isEqualToString:@"nodeContent"] ) {
temp_f = [node objectForKey:key];
}
}
}
xpathQueryString = @"//current_conditions/temp_c/@data";
nodes = PerformXMLXPathQuery(responseData, xpathQueryString);
//NSLog( @"nodes = %@", nodes );
for ( NSDictionary *node in nodes ) {
for ( id key in node ) {
if( [key isEqualToString:@"nodeContent"] ) {
temp_c = [node objectForKey:key];
}
}
}
temp = [NSString stringWithFormat:@"%@F (%@C)", temp_f, temp_c];
NSLog(@"temp = %@", temp);
// NSString *humidity;
xpathQueryString = @"//current_conditions/humidity/@data";
nodes = PerformXMLXPathQuery(responseData, xpathQueryString);
for ( NSDictionary *node in nodes ) {
for ( id key in node ) {
if( [key isEqualToString:@"nodeContent"] ) {
humidity = [node objectForKey:key];
}
}
}
NSLog(@"humidity = %@", humidity);
// NSString *wind;
xpathQueryString = @"//current_conditions/wind_condition/@data";
nodes = PerformXMLXPathQuery(responseData, xpathQueryString);
for ( NSDictionary *node in nodes ) {
for ( id key in node ) {
if( [key isEqualToString:@"nodeContent"] ) {
wind = [node objectForKey:key];
}
}
}
NSLog(@"wind = %@", wind);
// NSString *condition
xpathQueryString = @"//current_conditions/condition/@data";
nodes = PerformXMLXPathQuery(responseData, xpathQueryString);
for ( NSDictionary *node in nodes ) {
for ( id key in node ) {
if( [key isEqualToString:@"nodeContent"] ) {
condition = [node objectForKey:key];
}
}
}
NSLog(@"condition = %@", condition);
// Forecast Conditions
//NSMutableArray *days;
xpathQueryString = @"//forecast_conditions/day_of_week/@data";
nodes = PerformXMLXPathQuery(responseData, xpathQueryString);
for ( NSDictionary *node in nodes ) {
for ( id key in node ) {
if( [key isEqualToString:@"nodeContent"] ) {
[days addObject:[node objectForKey:key]];
}
}
}
NSLog(@"days = %@", days);
//NSMutableArray *icons;
xpathQueryString = @"//forecast_conditions/icon/@data";
nodes = PerformXMLXPathQuery(responseData, xpathQueryString);
for ( NSDictionary *node in nodes ) {
for ( id key in node ) {
if( [key isEqualToString:@"nodeContent"] ) {
[icons addObject:[NSString stringWithFormat:@"http://www.google.com%@", [node objectForKey:key]]];
}
}
}
NSLog(@"icons = %@", icons);
//NSMutableArray *temps;
NSMutableArray *highs = [[NSMutableArray alloc] init];
NSMutableArray *lows = [[NSMutableArray alloc] init];
xpathQueryString = @"//forecast_conditions/high/@data";
nodes = PerformXMLXPathQuery(responseData, xpathQueryString);
for ( NSDictionary *node in nodes ) {
for ( id key in node ) {
if( [key isEqualToString:@"nodeContent"] ) {
[highs addObject:[node objectForKey:key]];
}
}
}
xpathQueryString = @"//forecast_conditions/low/@data";
nodes = PerformXMLXPathQuery(responseData, xpathQueryString);
for ( NSDictionary *node in nodes ) {
for ( id key in node ) {
if( [key isEqualToString:@"nodeContent"] ) {
[lows addObject:[node objectForKey:key]];
}
}
}
for( int i = 0; i < highs.count; i++ ) {
[temps addObject:[NSString stringWithFormat:@"%@F/%@F", [highs objectAtIndex:i], [lows objectAtIndex:i]]];
}
NSLog(@"temps = %@", temps);
[highs release];
[lows release];
//NSMutableArray *conditions;
xpathQueryString = @"//forecast_conditions/condition/@data";
nodes = PerformXMLXPathQuery(responseData, xpathQueryString);
for ( NSDictionary *node in nodes ) {
for ( id key in node ) {
if( [key isEqualToString:@"nodeContent"] ) {
[conditions addObject:[node objectForKey:key]];
}
}
}
NSLog(@"conditions = %@", conditions);
[viewController updateView];
}