Je suis en train d'utiliser ce service Web dans mon application (juste pour essayer mes mains sur les services Web): http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit et ci-dessous est le code que je utilise:comment obtenir la valeur exacte de la réponse que je reçois du service 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];
}
Dans le NSString theXML je reçois une réponse qui a eu un peu erro aussi. est inférieure à la réponse que je reçois Débogueur:
WebServiceTutorial [474: 207] savon: ReceiverServer n'a pas pu demande de traitement. --- > Les données au niveau racine ne sont pas valides. Ligne 1, Position 1.
Merci,
Chaque fois que vous utilisez W3Schools assurez-vous de vérifier ce site aussi: http://w3fools.com/ – vikingosegundo
Je ne sais pas si cela fait partie du problème, mais 'NSString * params = [[ NSString alloc] initWithFormat: @ "Celsius = 32"]; 'pourrait donner un mauvais comportement, car vous ne passez pas dans un format. essayez 'params = @" Celsius = 32 "' à la place. BTW: Vous fuyez 'params' et' myWebData' – vikingosegundo