1

Web サービスにアクセスしたい、2 つのパラメーターを渡したい。

以下のコードを実行すると、次のエラーが表示されます。

@countryname not supplied

私はすでに2つのパラメータをtxtcityandとして渡していますtxtcountry

-(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 サービスは次の 3 つのパラメータを取ります。

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

これはあなたを助けることができるかもしれません。1 つのパラメータが含まれています

-(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];
    }
}

あなたのWebサービスは異なりますが、これでアイデアが得られるかもしれません。

于 2016-12-10T06:59:18.937 に答える