0

这个网络服务

我有这个网络服务。我写了下面的代码来从 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 *)解析器

这两个方法没有被调用。

但没有得到数据。我哪里可能出错,请帮我解决这个问题。

提前致谢。

4

2 回答 2

0

我认为您传递的 URL 参数不正确。当我尝试你的 WS URL 时,

http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?GetCitiesByCountryResult=印度

它给出了一个错误,缺少参数:CountryName

在此处输入图像描述

正确的 URL 参数似乎是CountryName而不是GetCitiesByCountryResult。这个修改后的 URL 给出了 XML 结果。在 WSDL 中检查需要为特定 WS 传递哪些参数。

http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?CountryName=India

希望有帮助!

于 2013-10-03T07:19:29.887 回答
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];
于 2013-10-03T07:39:55.043 回答