iLounge | All Things iPod, iPhone, iPad and Beyond

Saturday 7 April 2012

web services example using nsxml parser and image fetching with base64 classes.

firstly, take a new xcode project(single view based)

name the class file as

AreasListViewController.h and AreasListViewController.m

and then import base64.h and base64.m classes in to your project(google search)

As per my requirements for the project  my link is
http://10.13.11.45:8180/MapApps/MapAppWebServices?wsdl

this is completely wsdl format similar to xml one.




in ,h file. make the code as follows


#import "base64.h"

@interface AreasListViewController : UIViewController<NSXMLParserDelegate,UITableViewDataSource,UITableViewDelegate>
{
    NSMutableData *responseData;
    NSURLConnection *connection;    
    NSMutableArray *arrareasdata;
    NSString *tempStr;
    BOOL boolListofareasdata;
    NSMutableArray *array;
    NSArray *myArray;
}
@property(nonatomic,retain) NSMutableArray *arrareasdata;

in .m file


int i;
int count;

@interface AreasListViewController ()

@end

@implementation AreasListViewController
@synthesize arrareasdata;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
   
    arrareasdata = [[NSMutableArray alloc]initWithCapacity:0];
   
    NSString *soapReq=[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                       "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:sris=\"http://srisys.com/\">"
                       "<soapenv:Header/>"
                       "<soapenv:Body>"
                       "<sris:areasdata>"
                       "<arg0>2</arg0>"
                       "<arg1>1</arg1>"
                       "</sris:areasdata>"
                       "</soapenv:Body>"
                       "</soapenv:Envelope>"];
   
    NSString *strReqLength = [NSString stringWithFormat:@"%d",[soapReq length]];
    NSURL *url = [NSURL URLWithString:@"http://10.13.11.45:8180/MapApps/MapAppWebServices?wsdl"];
   
    //setting header tags
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
   
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
   
    [request addValue:strReqLength forHTTPHeaderField:@"Content-Length"];
   
   
    [request addValue:@"http://srisys.com/MapAppWebServices/areasdataRequest" forHTTPHeaderField:@"SOAPAction"];
   
   
    //setting body tags
    NSData *data= [soapReq dataUsingEncoding:NSUTF8StringEncoding];
   
    [request setHTTPBody:data];
   
    [request setHTTPMethod:@"POST"];
    //[activityIndicator startAnimating];
   
   
    connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
   
   
   
    [super viewDidLoad];
   
   
   
    // Do any additional setup after loading the view from its nib.
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
   
    responseData = [[NSMutableData alloc]init];
   
    NSLog(@"didReceiveResponse");
   
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
   
   
    [responseData appendData:data];
   
    NSLog(@"didReceiveData");
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
   
    NSString *strResponse =[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
   
    NSLog(@"response is %@",strResponse);
   
    NSLog(@"connectionDidFinishLoading..%@",responseData);
   
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:responseData];
   
    parser.delegate = self;
   
    [parser parse];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
 namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
   
    if ([elementName isEqual:@"areaName"]) {
       
        boolListofareasdata=YES;
       
    }
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
   
    if (boolListofareasdata) {
       
        [arrareasdata addObject:string];
       
        NSLog(@"ListOfRegionsResult is...%@",arrareasdata);
       
 }
   
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
 namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
   
    if ([elementName isEqual:@"areaName"]) {
       
        boolListofareasdata=NO;
       
    }
    if ([elementName isEqual:@"areaIcon"]) {
        boolListofareasdata=NO;
        array=[[NSMutableArray alloc]init];
        count = [array count];
       
        for (i = 0; i < count; i++)
            NSLog (@"Element %i = %@", i, [array objectAtIndex: i]);
       //[array componentsJoinedByString:tempStr];
       // NSString *data=[[NSString alloc]initWithString:tempStr];
       //tempStr=[array objectAtIndex:i];
        //[tempStr addObject:array];
       
        Byte inputData[[tempStr lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
        [[tempStr dataUsingEncoding:NSUTF8StringEncoding] getBytes:inputData];
        size_t inputDataSize=(size_t)[tempStr length];
        size_t outputDataSize=EstimateBas64DecodedDataSize(inputDataSize);
        Byte outputData[outputDataSize];
        Base64DecodeData(inputData, inputDataSize, outputData, &outputDataSize);
        NSData *theData=[[NSData alloc]initWithBytes:outputData length:outputDataSize];
        array=[NSMutableArray arrayWithObject:theData];
        }
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
   
    UITableView *tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
   
    tab.delegate = self;
    tab.dataSource = self;
   
    [self.view addSubview:tab];
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return [arrareasdata count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    UITableViewCell *cell = [[UITableViewCell alloc] init];   
    cell.textLabel.text=[arrareasdata objectAtIndex:indexPath.row];
    cell.imageView.image=[array objectAtIndex:indexPath.row];
    tableView.separatorColor=[UIColor greenColor];
    cell.textLabel.textColor=[UIColor blackColor];
   
    return cell;
   
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}



make the appropriate connections and debug.

i hope this code helps everyone alot. thank you

1 comment: