如何使用 NSXML 解析 Google 天气 API?
我想使用 NSXML 解析 google 天气 API,所以请给我一些指导。
这是我的网址
,我已经采取了这样的步骤:
NSURL *url = [NSURL URLWithString:@"http://www.google.com/ig/api?weather=Ahemdabad"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection){
webData = [[NSMutableData data] retain];
NSLog( @"connection established");
}
else {
NSLog(@"theConnection is NULL");
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connectio
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"thexml=============>%@", theXML);
[theXML release];
if(parser)
{
[parser release];
}
parser = [[NSXMLParser alloc]initWithData:webData];
[parser setDelegate: self];
[parser setShouldResolveExternalEntities: YES];
[parser parse];
[connection release];
[webData release];
}
i want to parse google weather API using NSXML so please give me some Guidance for this.
This is My url
and i have taken such kind of steps:
NSURL *url = [NSURL URLWithString:@"http://www.google.com/ig/api?weather=Ahemdabad"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection){
webData = [[NSMutableData data] retain];
NSLog( @"connection established");
}
else {
NSLog(@"theConnection is NULL");
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connectio
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"thexml=============>%@", theXML);
[theXML release];
if(parser)
{
[parser release];
}
parser = [[NSXMLParser alloc]initWithData:webData];
[parser setDelegate: self];
[parser setShouldResolveExternalEntities: YES];
[parser parse];
[connection release];
[webData release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嘿 ankit,如果对您有帮助,您可以获取此代码,无需建立连接,
只需使用此方法即可
,也可以使用其他支持方法
-(void)parser:(NSXMLParser*)parserfoundCharacters:(NSString*)string {
if(tmpString==nil && !didGetHTML){
tmpString=[[NSString alloc] initWithString:string];
} 否则 if(!didGetHTML){
NSString *t=[NSString stringWithString:tmpString];
if([tmpString keepCount]>0) { [tmpString release]; tmpString=nil; }
tmpString=[[NSString alloc] initWithFormat:@"%@%@",t,string];
}
并
简单地从您编写此方法的类中调用 initwith url 方法,
只需提供根标记对象标记和特定对象标记的元素标记,并在之后提供选择器,然后在字典中获取响应,然后将其接收数组并根据您的键值显示结果
hey ankit you can get this code if at all its helpful to you no need to establish connection
just use this method
also the other supporting method
-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString*)string {
if(tmpString==nil && !didGetHTML){
tmpString=[[NSString alloc] initWithString:string];
} else if(!didGetHTML){
NSString *t=[NSString stringWithString:tmpString];
if([tmpString retainCount]>0) { [tmpString release]; tmpString=nil; }
tmpString=[[NSString alloc] initWithFormat:@"%@%@",t,string];
}
}
and simply call the initwith url method from which ever class you have written this method
just you have to give root tag object tag and element tag of a particular object tag and also give selector after that take the response in dictionary and they take it in array and display the result according to your value for key