0

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
Community
  • 1
  • 1
ginosal
  • 35
  • 1
  • 1
  • 6

2 Answers2

0

Can't you create a NSString with the list of integers and separate them by , and send that instead?

NSMutableString *mutableList = [[NSMutableString alloc ]initWithString:@"<q14:ArrayOfint>"];
//I'll say like this because i don't know if you added them as NSNumber or NSString
for(int i = 0 ;i < idProdotti.count;i++)
{
NSInteger value = [[idProdotti objectAtIndex:i]integerValue];

  [mutableList appendFormat:@"<arr:int>%d</arr:int>",value];

 }
[mutableList appendFormat:@"</q14:ArrayOfint>"];

and send mutableList then release it; //second one for qtaProdotti

NSMutableString *mutableList1 = [[NSMutableString alloc ]initWithString:@"<q15:ArrayOfint>"];
//I'll say like this because i don't know if you added them as NSNumber or NSString
for(int i = 0 ;i < qtaProdotti.count;i++)
{
NSInteger value = [[qtaProdotti objectAtIndex:i]integerValue];

  [mutableList1 appendFormat:@"<arr:int>%d</arr:int>",value];

 }
[mutableList1 appendFormat:@"</q15:ArrayOfint>"];

and send mutableList1 then release it;

soryngod
  • 1,827
  • 1
  • 14
  • 13
  • Ok thanks, I'll try tomorrow ... if there are other ideas are welcome! – ginosal Jul 07 '13 at 08:44
  • It doesn't work in this way, there is the error "Expected 'element' found 'text'", but the wsdl only accepts a list of integers without additional tags. I don't know how to do. – ginosal Jul 08 '13 at 07:50
  • Unfortunately, no. I know only that the method "CalcolaImporto" accepts as idProdotti and how qtaProdotti two lists of integers. The wsdl was created using Visual Studio 2010 and the method is: "public double CalcolaImporto(int idPaesePrelievo, int idPaeseDest, List idProdotti, List qtaProdotti)". – ginosal Jul 08 '13 at 08:12
  • You need to know how is the webservice created, so you now how to send the parameters. – soryngod Jul 08 '13 at 08:26
  • you are surely not sending it correctly, you might have to change what i said with a format. – soryngod Jul 08 '13 at 08:32
  • The wsdl for the method is: – ginosal Jul 08 '13 at 08:37
  • Unfortunately the edited post doesn't work...there are no errors, but the list is empty. – ginosal Jul 08 '13 at 08:45
  • I have searched the web and the edited post might work, so please try it for the first idProdotti. – soryngod Jul 08 '13 at 08:49
  • Thank you very much! You have been very kind in answering, this afternoon I public the correct code, thanks again. – ginosal Jul 08 '13 at 11:19
0

I solved in this way:

  • I used "SoapUI", a graphical interface to interact with the web services, and I rewrote the SOAP message;

  • I used two lists similar to those that showed me "Soryngod".

Here's the correct code:

NSMutableString *idLista = [[NSMutableString alloc] init];
for(int i=0; i<idProdotti.count; i++) {
    NSInteger value = [[idProdotti objectAtIndex:i] integerValue];
    [idLista appendFormat:@"<arr:int>%d</arr:int>",value];
}

NSMutableString *qtaLista = [[NSMutableString alloc] init];
for(int i=0; i<qtaProdotti.count; i++) {
    NSInteger value = [[qtaProdotti objectAtIndex:i] integerValue];
    [qtaLista appendFormat:@"<arr:int>%d</arr:int>",value];
}

NSString *soapMsg = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                 "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem="http://tempuri.org/\" xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays\">"
                 "<soapenv:Body>"
                 "<tem:CalcolaImporto>"
                 "<tem:idPaesePrelievo>%d</tem:idPaesePrelievo>"
                 "<tem:idPaeseDest>%d</tem:idPaeseDest>"
                 "<tem:idProdotti>%@</tem:idProdotti>"
                 "<tem:qtaProdotti>%@</tem:qtaProdotti>"
                 "</tem:CalcolaImporto>"
                 "</soapenv:Body>"
                 "</soapenv:Envelope>", idPaesePrelievo, idPaeseDest, idLista, qtaLista];

[...]
ginosal
  • 35
  • 1
  • 1
  • 6
  • @soryngod can you help me in this post please? [link](http://stackoverflow.com/questions/17569592/uipickerview-foreground) – ginosal Jul 10 '13 at 12:19