废话少讲,直接上代码!你懂的
<!-- lang: cpp -->
@interface pyViewController ()<NSXMLParserDelegate>
{
NSMutableString *_string ;
NSMutableArray *_array;
pyWeather *_w;
}
@end
/*
项目分析
1.先取出数据
2.再将数据显示到地图上,
/
@implementation pyViewController
-(void)xmlHTTPRequestWithString:(NSString *)str {
//1.url
NSString *urlString = @"http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather";
NSURL *url = [NSURL URLWithString:urlString];
//2.urlrequest
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0f];
urlRequest.HTTPMethod = @"post";
NSString *datastring = [NSString stringWithFormat:@"theCityCode=%@&theUserID=",str];
urlRequest.HTTPBody = [datastring dataUsingEncoding:NSUTF8StringEncoding] ;
//3.发送
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
}];
}
-(void)parserDidStartDocument:(NSXMLParser *)parser
{
_string = [NSMutableString string];
_array = [NSMutableArray array];
}
-(void)parser:(NSXMLParser )parser didStartElement:(NSString )elementName namespaceURI:(NSString )namespaceURI qualifiedName:(NSString )qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"ArrayOfString"]) {
[_array removeAllObjects];
}
}
-(void)parser:(NSXMLParser )parser foundCharacters:(NSString )string
{
[_string appendString:string];
}
-(void)parser:(NSXMLParser )parser didEndElement:(NSString )elementName namespaceURI:(NSString )namespaceURI qualifiedName:(NSString )qName
{
if ([elementName isEqualToString:@"string" ]) {
//NSLog(@"%@",_string);
NSString *result = [NSString stringWithString: _string];
[_array addObject:result];
}
}
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"%@",_string);
}
-(void)parser:(NSXMLParser )parser parseErrorOccurred:(NSError )parseError
{
NSLog(@"the reason of faile is %@",parseError.localizedDescription);
}
如果有什么不懂的地方,你可以联系本人