废话少讲,直接上代码!你懂的
@interface pyViewController ()
{ NSMutableString *_string ; NSMutableArray *_array; pyWeather *_w; } @end
/** 项目分析 1.先取出数据 2.再将数据显示到地图上, */
@implementation pyViewController
- (void)viewDidLoad { [super viewDidLoad]; [self xmlHTTPRequestWithString:@"北京"]; }
-(void)xmlHTTPRequestWithString:(NSString *)str { //1.url NSString *urlString = @""; 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]; }]; }
#pragma mark -xml解析方法 #pragma mark 开始文档解析 -(void)parserDidStartDocument:(NSXMLParser *)parser {
_string = [NSMutableString string];_array = [NSMutableArray array];
}
#pragma mark 开始节点解析 -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"ArrayOfString"]) { [_array removeAllObjects];}
} #pragma mark 真正解析 -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
[_string appendString:string];
}
#pragma mark 完成借点解析 -(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];}
}
#pragma mark 结束解析 -(void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(@"%@",_string);
}
#pragma mark 解析错误时 -(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { NSLog(@"the reason of faile is %@",parseError.localizedDescription); }
如果有什么不懂的地方,你可以联系本人