Saturday, May 5, 2012

SOAP Web Services in iPhone/iPad

Here i posted the code. simply follow the steps.....

Create one button in xib and implemented with name -(IBAction) read_data,
bcz when we press the button then it fired soap webservice.

firstView.h file

@interface firstView : UIViewController {


NSMutableArray *list;
UILabel *soapValue;
}
- (IBAction) read_data;
@end

firstView.m file

@implementation firstView


- (IBAction) read_data
{
 NSString *name=@"50";     // Here name is, we providing value. so u change to ur provide value.

NSString *soapMessage = [NSString stringWithFormat:
@"\n"
"\n"
"\n"
"n"
"%@n"
"n"
"\n"
"\n",name
];

NSLog(@"soap Message= %@",soapMessage);
NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"];    //Here is the your server URL

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];   

NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
}

-(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
{
NSLog(@"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"theXML %@",theXML);     //theXML is the ur server XML
[theXML release];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:webData];
[parser setDelegate:self];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[parser release];
}


-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
{
if([elementName isEqualToString:@"CelsiusToFahrenheitResult"])
{
list = [[NSMutableArray alloc] init];
}
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
{
if([elementName isEqualToString:@"CelsiusToFahrenheitResult"])
{
list = [[NSMutableArray alloc] init];
}
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{
if(string)
{
NSLog(@"String Value= %@",string);    //this string value is the response value splitted from xml
}
}

Thanks 
----------------------

Ravi Kumar K



No comments:

Post a Comment