1

我想访问一个网络服务,想传递 2 个参数。

当我运行下面的代码时,会显示此错误:

@countryname not supplied

我已经将 2 个参数传递为txtcitytxtcountry

-(IBAction)FindWords:(id)sender
{
NSString *soapMsg =    
[NSString stringWithFormat:  

 @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"

 "<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>"

 "<GetWeather xmlns=\"http://www.webserviceX.NET/\">"

 "<CityName>%@</CityName>"

 "<CountryName>%@</CountryName>"

 "</GetWeather>"  

 "</soap:Body>"

 "</soap:Envelope>", txtCity.text,txtCounrty.text];

//---print it to the Debugger Console for verification---
NSLog(@"%@",soapMsg);

NSURL *url = [NSURL URLWithString:
              @"http://www.webservicex.net/globalweather.asmx"];

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
              //---set the various headers---
              NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
 NSLog(@"WebData....%@",soapMsg);
              [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
              [req addValue:@"http://www.webserviceX.NET/GetWeather" forHTTPHeaderField:@"SOAPAction"];

               [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

               //---set the HTTP method and body---

               [req setHTTPMethod:@"POST"];

               [req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

               //---start animating--
               [activityIndicator startAnimating];

               conn = [[NSURLConnection alloc] initWithRequest:req                                                          delegate:self];

               if(conn)
               {                   
               webData = [[NSMutableData data] retain];

                    NSLog(@"WebDatanew....%@",webData);

               }
}
4

5 回答 5

2

只需包含 ASIHTTPRequest。它更易于使用。

http://allseeing-i.com/ASIHTTPRequest/
于 2012-05-19T11:15:18.227 回答
0

使用 ASIHTTP 请求和 JSON 库与您的 Web 服务进行通信。JSON 比 XML 提要好得多,而且很容易处理。

在此处下载 JSON 库表单

这是 ASIHTTP 请求的文档:- http://allseeing-i.com/ASIHTTPRequest/

希望这会帮助你。谢谢

于 2012-05-21T09:14:44.940 回答
0

更改您的 soapMsg 格式:

NSString *soapMsg = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                         "<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/\">\n"
                         "<soap:Body>\n"
                         "<GetWeather xmlns=\"http://www.webserviceX.NET\">\n"
                         "<CityName>%@</CityName>"
                         "<CountryName>%@</CountryName>\n"
                         "</GetWeather>\n"
                         "</soap:Body>\n"
                         "</soap:Envelope>\n",txtCity.text,txtCounrty.text];
于 2012-05-21T05:37:25.337 回答
0

Web 服务采用三个参数:

rw_app_id: The unique identifier for the app. If you’ve been following along with the previous tutorial, there should be only one entry so far, App ID #1.
code: The code to attempt to redeem. This should be a string that’s entered by the user.
device_id: The device ID that is attempting to redeem this code. We can get this with an easy API call.

使用:- ASIHTTPRequest

于 2012-05-19T11:05:03.050 回答
0

可能这可以帮助你。它包含一个参数

-(void)serverconnection{

   NSString *CountryName=@"India";

    NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                             "<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>"
                             "< GetWeather xmlns=\"http://tempuri.org/\">"
                             "<CountryName>%@</CountryName>"
                             "</GetWeather >"
                             "</soap:Body>"
                             "</soap:Envelope>",CountryName];

    NSURL *myNSUObj=[NSURL URLWithString:@"http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry"];
    // NSURLRequest *myNSURequestObj=[NSURLRequest requestWithURL:myNSUObj];

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:myNSUObj];
    NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://tempuri.org/GetWeather" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    myNSUConnectionObj=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
    NSLog(@"Data =%@",myNSUConnectionObj);
    if(myNSUConnectionObj)
    {

        NSLog(@"successful connection");
        myNSMDataFromServer=[[NSMutableData alloc]init];
    }
}

您的网络服务有所不同,但这可能会给您一些想法。

于 2016-12-10T06:59:18.937 回答