Friday, 29 March 2013

How to parse WSDL web service, without using XML.

Hello All first of before starting the blog let me introduce my self,

My name is Rizwan and I am iOS and Mac application developer with having 3 years of experience,
And reason to write this blog is one of my friend name is Krupanshu has tell me that "If you have this trick then why don't you write the blog for it" thats why I am write this blog.

And I am not a professional blogger so Sorry for any mistake.

before a few month I have parse the WSDL web service with Sudz-c and also through Make a message body and pass into HTTP post but these all is bit lengthy process and also its taking more time so my personal opinion is this is nit good for us.

So here I am  sharing my trick to parse the WSDL service without using XML parsing.
So I am going Point-To-Point via source code so you just ketch the my idea and then you can just modify my source according to your requirement.

STEP :- 1  Create NSUrlConnection like bellow


   NSArray *objects = [NSArray arrayWithObjects:@"gmail@gmail.com",@"123456",nil]; //
    NSArray *keys = [NSArray arrayWithObjects: @"user_name", @"Password", nil];
    NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
 
    NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:questionDict forKey:@""];//Her you can give your root name for JSON Object
 
    NSString *jsonRequest = [jsonDict JSONRepresentation];
 
    NSLog(@"jsonRequest is %@", jsonRequest);
 
    NSURL *url = [NSURL URLWithString:@"http://YOURLINK/YOUR_METHOD_NAME"];
//in the URL please first write your WSDL link and THEN write your method name followed by / "forward Slash"for example
YourWSDLLink/Login

 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:0.0];
 
 
    NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
 
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: requestData];
 
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    if (connection) {
        webData = [[NSMutableData data] retain];
    }
 
}

STEP -: 2 Write your NSURLConnection delegate methods


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

-(void) connection:(NSURLConnection *) connection //Repeative call method and append data to webData
    didReceiveData:(NSData *) data {
    [webData appendData:data];
}

-(void) connection:(NSURLConnection *) connection//If error occure error should be displayed
  didFailWithError:(NSError *) error {
    //    [appDel.HUD hide:YES];
    NSLog(@"connection Problem");
    [webData release];
    [connection release];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *responseString = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",responseString);
    [webData release];
    NSDictionary *dict = [responseString JSONValue];
    resultarr = [dict objectForKey: @"your Key"];
    NSLog(@"%@",resultarr);
    [responseString release];
 
}

In the connectionDidFinishLoading method you can see your Perfect response.

And also import required file for EX. JSON, Connection delegate etc etc

Just Take my POINT-TO-POINT Idea.

And Also make sure that in server side (Your service provider) Must read JSON Object otherwise this trick will Not WORK

Thanks,
Rizwan.