我有这个网络服务。我写下以下代码从webservice获取数据。但没有得到数据。
我试过这段代码。
-(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 *)解析器
这两种方法没有被调用。
但没有得到数据。哪里可能出错我请帮帮我。
提前感谢。
答案 0 :(得分:0)
我认为您传递的网址参数不正确。当我尝试你的WS URL时,
http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?GetCitiesByCountryResult=India
出现错误,缺少参数:CountryName
正确的网址参数似乎是 CountryName ,而不是 GetCitiesByCountryResult 。此修改后的URL提供XML结果。检查WSDL需要为特定WS传递哪些参数。
http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?CountryName=India
希望有所帮助!
答案 1 :(得分:0)
在convertFrom方法中尝试这样。
NSString *soapMessage = [NSString stringWithFormat:@"<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>
<GetCitiesByCountry xmlns="http://www.webserviceX.NET">
<CountryName>%@</CountryName>
</GetCitiesByCountry>
</soap:Body>
</soap:Envelope>",CountryName];
NSURL *url = [NSURL URLWithString:http://www.webservicex.net/globalweather.asmx];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString * msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://www.webserviceX.NET/GetCitiesByCountry" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSData * data = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];