我想访问一个webservice,想传递2个参数。
当我运行下面的代码时,会显示以下错误:
@countryname not supplied
我已将2个参数传递为txtcity
和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);
}
}
答案 0 :(得分:2)
只包括ASIHTTPRequest.Its更容易使用。
http://allseeing-i.com/ASIHTTPRequest/
答案 1 :(得分: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
答案 2 :(得分: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];
答案 3 :(得分:0)
使用ASIHTTP请求和JSON库与您的Web服务进行通信。 JSON比XML feed更好,而且非常容易处理。
下载JSON库form Here
以下是ASIHTTP申请的文件: - http://allseeing-i.com/ASIHTTPRequest/
希望这会对你有所帮助。谢谢
答案 4 :(得分: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];
}
}
您的网络服务有所不同,但这可能会让您有所了解。