Database Reference
In-Depth Information
Well, that was certainly easy! Except, actually, we're just getting started. As anyone
who has ever used SOAP bindings knows, making them work involves a bunch of
guesswork and a menagerie of intermediate objects of no particularly obvious purpose.
Let's look at the code that you'd need to write in order to consume this service, shown
in Example 4-11 .
Example 4-11. Consuming a SOAP web service
- (IBAction)showSOAPWeather:(id)sender {
WeatherSoapBinding *binding =
[WeatherSvc WeatherSoapBinding];
binding.logXMLInOut = YES;
WeatherSvc_GetWeather *params = [[WeatherSvc_GetWeather alloc] init];
params.City = @"Derry, NH";
WeatherSoapBindingResponse *response =
[binding
GetWeatherUsingParameters:params];
for (id bodyPart in response.bodyParts) {
if ([bodyPart isKindOfClass:[SOAPFault class]]) {
NSLog(@"Got error: %@",
((SOAPFault *)bodyPart).simpleFaultString);
continue;
}
if ([bodyPart isKindOfClass:
[WeatherSvc_GetWeatherResponse class]]) {
WeatherSvc_GetWeatherResponse *response = bodyPart;
NSLog(@"Forecast: %@", response.GetWeatherResult);
}
}
}
The general pattern here, as with most SOAP bindings, is that you get a binding object
(which has all the information about things such as the URI of the service). Then you
get a request object, set all the parameters of the request, then call the appropriate
method on the binding (in this case, GetWeatherUsingParameters ), handing in the re-
quest. There are two versions of each request method, one synchronous (the one we're
using in this example), and one asynchronous (which uses the same kind of callback
mechanism we've used in the JSON and XML examples). We also set the logXMLIn
Out flag to true, so we'll get a look at what we're sending and receiving in pure SOAP
form, this can be very helpful figuring out where data is hiding in the response.
When the response returns, you have to iterate over the body parts, checking for SOAP
faults. If the body part isn't a SOAP fault, you have to cast it to the appropriate object.
And here's where the fun begins, because there's nothing to tell you what the possible
types are, except for diving through the generated code looking for where the elements
are populated. But, if you do it all right, you'll get results.
2011-11-18 08:16:17.365 BuggyWhipChat[69775:fb03] Forecast: Sunny
 
Search WWH ::




Custom Search