我想使用此Web服务在我的应用程序(只是为了尝试Web服务我的手):http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit以下是我使用的代码:如何从我从Web服务获得的响应中获得准确的值?
- (void)viewDidLoad {
[super viewDidLoad];
// create a soap Message which is given in your required web service
// create a url to your asp.net web service.
NSURL *tmpURl=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit"]];
// create a request to your asp.net web service.
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:tmpURl];
NSString * params = [[NSString alloc] initWithFormat:@"Celsius = 32"];
myWebData = [[NSMutableData alloc] init];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
}
// a method when connection receives response from asp.net web server
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[myWebData setLength: 0];
}
// when web-service sends data to iPhone
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[myWebData appendData:data];
NSString *receivedDataString = [[NSString alloc] initWithData:myWebData encoding:NSUTF8StringEncoding];
NSLog(@"%@",receivedDataString);
}
// when there is some error with web service
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
// when connection successfully finishes
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// check out your web-service retrieved data on log screen
NSString *theXML = [[NSString alloc] initWithBytes: [myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",theXML);
[theXML release];
}
在的NSString theXML我得到了一些响应也有一些错误。 下面是我收到的调试器的响应:
WebServiceTutorial [474:207]皂:ReceiverServer无法 处理请求。 --- >数据在 根级别无效。 1号线, 位置 1.
感谢,
当曾经使用W3Schools的请务必检查这个网站太:http://w3fools.com/ – vikingosegundo
不知道,如果这是问题的一部分,但'的NSString * PARAMS = [[ NSString alloc] initWithFormat:@“Celsius = 32”];'可能会给出不好的行为,因为你没有传入格式。尝试'params = @“Celsius = 32”'代替。顺便说一句:你正在泄漏'params'和'myWebData' – vikingosegundo