I'm creating an app iOS that requires the connection to a web service such as "http://xxx.xxx.xx.xxx/WebSiteServices.svc?wsdl".
The app allows to make a quote for a trip with luggage, after selecting some fields:
Country of origin (list of countries of origin);
Country of destination (list of countries of destination);
5 identification fields for 5 id of luggage, each allowing to select the number of bags with different id.
To communicate with the web service I made a SOAP call as explained in the link: "iPhone interaction with ASP.NET WebService."
I succeeded to receive lists of nations and luggage, now I cannot send the selected data to the web service to invoke the "calcolaImporto" (Calculate Amount) method. I have to send in the SOAP message:
idPaesePrelievo: Id of the country of origin (an Integer: OK, I succeded);
idPaeseDest: Id of the destination country (an Integer: OK, I succeded);
idProdotti: List of integers that identifies the id of the selected storage (PROBLEM: I cannot send the array);
qtaProdotti: List of integers that identifies the amount of luggage id selected for each of the first list (PROBLEM: I cannot send the array).
The two lists are not connected to each other, but I cannot send to the web service these two arrays.
The arrays in the web service are composed by two lists of integers, even if the two array of Xcode are composed by two lists of object id (I also tried doing the cast from id to int, but nothing).
The method is accessed, but the result is '0' because it is not checked any luggage: how can I do?
PLEASE help me, thank you!
Below I have posted the code of “ViewController.m”:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize amount, idPaeseDest, idPaesePrelievo, idProdotti, qtaProdotti;
/* amount (TEXTFIELD), idPaeseDest (INT), idPaesePrelievo (INT), idProdotti (NSMUTABLEARRAY), qtaProdotti (NSMUTABLEARRAY) */
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)calcolaImporto:(id)sender {
// Create the SOAP message
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>"
"<CalcolaImporto xmlns=\"http://tempuri.org/\">"
"<idPaesePrelievo>%d</idPaesePrelievo>"
"<idPaeseDest>%d</idPaeseDest>"
"<idProdotti>%@</idProdotti>"
"<qtaProdotti>%@</qtaProdotti>"
"</CalcolaImporto>"
"</soap:Body>"
"</soap:Envelope>", idPaesePrelievo, idPaeseDest, idProdotti, qtaProdotti];
// Create the URL
NSURL *url = [NSURL URLWithString: @"http://xxx.xxx.xx.xxx/WebSiteServices.svc?wsdl"];
// Create the request
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/IBagExpressServices/CalcolaImporto" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}
}
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response {
[webData setLength: 0];
}
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data {
[webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error {
UIAlertView *errore = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Connection problem" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errore show];
[errore release];
[webData release];
[connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSLog(@"Ok. Byte: \n %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"theXML: \n %@", theXML);
[theXML release];
if (xmlParser) {
[xmlParser release];
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
[connection release];
[webData release];
}
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {
if ([elementName isEqualToString:@"CalcolaImportoResponse"])
soapResults = [[NSMutableString alloc] init];
elementFound = YES;
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string {
if (elementFound)
[soapResults appendString:string];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"CalcolaImportoResult"])
amount.text=soapResults;
elementFound = FALSE;
}
- (void)dealloc {
[amount release];
[soapResults release];
[super dealloc];
}
@end