Yassine Khabbazia
Yassine Khabbazia

Reputation: 41

consume .Net web Service in ios 7 xcode 5.0.2

I have created a .net web service that accept a parameter (int DishId) and returns an XML file with the attributes:

<Dish xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Menu_Id>3</Menu_Id>
<Dish_Name>DNN</Dish_Name>
<Dish_Description>DD</Dish_Description>
<Dish_Price>1555</Dish_Price>
<Dish_Ingredients>DI</Dish_Ingredients>
<Dish_Picture>images/smile.png</Dish_Picture>
<Dish_Category>3</Dish_Category>
</Dish>

So after some search on how to consume the web service in iOS Xcode 5.0.2. I followed the SOAP approach. I followed this link :Consuming XML Web Services in iPhone Applications

I have created a new page that contains Label, Text Field ("dishNumber in the code below") , button, Activity Indicator.

So for the button action I created the below code:

- (IBAction)buttonClicked:(id)sender
{
    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>\
     <GetDish xmlns=\"http://www.tempuri.org/\">\
     <dishId>%@</dishId>\
     </GetDish>\
     </soap:Body>\
     </soap:Envelope>",
     dishNumber.text
     ];

    //---print it to the Debugger Console for verification---
    NSLog(soapMsg);

    NSURL *url = [NSURL URLWithString: @"http://urllocalhost/mywebservice.asmx"];

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

    //---set the headers---
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];

    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [req addValue:@"http://www.tempuri.org/GetDish" forHTTPHeaderField:@"SOAPAction"];
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

    //---set the HTTP method and body---
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    [activityIndicator startAnimating];

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];

    if (conn)
    {
        webData = [NSMutableData data];
    }
}

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response
{
    [webData setLength: 0];
}

-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data
{
    [webData appendData:data];
}


-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);

    NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes]
                                                length:[webData length]
                                              encoding:NSUTF8StringEncoding];
    //---shows the XML---
    NSLog(theXML);

    [activityIndicator stopAnimating];
}

So after running this code, all appears Good. But the issue here is the system can just return the integer attributes from my XML file such as Menu_Id, dish_Price, dish_category with the values all 0.

Here is the message obtained with NSLog after clicking on the button:

2013-12-30 11:24:00.608 ws[6468:70b] soap message :<?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><GetDish xmlns="http://www.tempuri.org/"><dishId>3</dishId></GetDish></soap:Body></soap:Envelope>

2013-12-30 11:24:01.489 ws[6468:70b] DONE. Received Bytes: 420

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetDishResponse xmlns="http://tempuri.org/"><GetDishResult><Menu_Id>0</Menu_Id><Dish_Price>0</Dish_Price><Dish_Categorie>0</Dish_Categorie></GetDishResult></GetDishResponse></soap:Body></soap:Envelope>

So can any one please help on this. Thanks in advance.

Upvotes: 0

Views: 2663

Answers (3)

Charlie
Charlie

Reputation: 5

conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; please put the identifier name of conn i.e.

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; also mention its delegate

Upvotes: 0

Tron5000
Tron5000

Reputation: 854

Have you considered using a tool to generate proxy classes for your Xcode project? Use of proxy classes will abstract the shuttling of xml back and forth with your service.

I've not had any experience with these particular tools but hopefully they'll make it easier for you to consume your service:

wsdl2objc - https://code.google.com/p/wsdl2objc/

Sudzc (online utility) - http://www.sudzc.com/

Wsdl2Code (online utility) - http://www.wsdl2code.com/pages/home.aspx

Upvotes: 1

CRDave
CRDave

Reputation: 9285

There can be only one possible problem SOAP envelope.

Replace soapMsg with this line and tell me what u get.

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>"
                     "<GetDish xmlns=\"http://tempuri.org/\">"
                     "<dishId>%@</dishId>"
                     "</GetDish>"
                     "</soap:Body>"
                     "</soap:Envelope>",
                     dishNumber.text
                     ];

Upvotes: 0

Related Questions