我有这个网络服务。我写了下面的代码来从 web 服务获取数据。但不获取数据。
我试过这段代码。
-(CGFloat)convertFrom:(NSString*)CountryName
{
NSString * urlString = [NSString stringWithFormat:@"http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?CountryName=%@",CountryName];
NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[req setValue:@"http://www.webserviceX.NET" forHTTPHeaderField:@"Host"];
[req setHTTPMethod:@"GET"];
NSData * data = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSLog(@"%@",data);
xmlResultString = [[NSMutableString alloc]init];
NSLog(@"%@",xmlResultString);
parser = [[NSXMLParser alloc]initWithData:data];
[parser setDelegate:self];
[parser parse];
return result;
}
- (IBAction)ParseTapped:(id)sender
{
//NSString * urlString = [NSString stringWithFormat:@"http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=%@&ToCurrency=%@",fromCurrency,toCurrency];
[self convertFrom:@"India"];
//return result;
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[xmlResultString appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
}
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
result = [xmlResultString floatValue];
NSLog(@"%f",result);
}
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
result = -1;
}
foundCharacters:(NSString *)string
(NSXMLParser *)解析器
这两个方法没有被调用。
但没有得到数据。我哪里可能出错,请帮我解决这个问题。
提前致谢。