Sunday, August 18, 2013

Creating Asynchronous request for Soap WebService with ASIHTTPRequest

Follow the steps below: -

1) Visit: - http://allseeing-i.com/ASIHTTPRequest/Setup-instructions

Go to this link and download classes which are as told.

2) Read this code for easy understanding:- 

//
//  demoViewController.m
//  SendAsynchronousRequest
//
//  Created by Ashish on 8/17/13.
//  Copyright (c) 2013 ashish. All rights reserved.
//

#import "demoViewController.h"
#import "ASIHTTPRequest.h"
#import "ASIHTTPRequestDelegate.h"
#import "ASINetworkQueue.h"
#import "ASIFormDataRequest.h"

@interface demoViewController ()

@end


@implementation demoViewController

NSMutableString *getData;
NSMutableString *getData1;
NSMutableString *getData2;

//XML Parser
//---xml parsing---
NSXMLParser *xmlParser;
NSMutableString *soapResults;
BOOL elementFound;
int flag;
NSMutableData *webData;


@synthesize activity,MainCatId;

-(void)connectionString
{
   
    [activity startAnimating];
   
    NSURL *url=[NSURL URLWithString:@""];
   
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
   
    [request setDelegate:self];
   
    [request setRequestMethod:@"POST"];
   
    [request addRequestHeader:@"Content-Type" value:@"text/xml"];
   
    [request addRequestHeader:@"SOAPAction" value:@"http://tempuri.org/Search"];
   
    NSString *soapString=[NSString stringWithFormat:
                          @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
]";
    NSLog(@"%@",soapString);
  
   

    NSString *msgLength =[NSString stringWithFormat:@"%d", [soapString length]];
   
    [request addRequestHeader:@"Content-Length" value:msgLength];
   
    [request appendPostData:[soapString dataUsingEncoding:NSUTF8StringEncoding]];
   
    [request setDelegate:self];
   
    [request setDidFinishSelector:@selector(requestDone:)];
   
    [request setDidFailSelector:@selector(requestWentWrong:)];
   
    [request startAsynchronous];
   
   
    if (request)
    {
        webData = [NSMutableData data];
       
        //NSLog(@"response= %@",responseData);
    }

   
   
}


-(void)parseUser:(NSString *)xml{
   
   
    NSData* data=[xml dataUsingEncoding:NSUTF8StringEncoding];
   
    xmlParser = [[NSXMLParser alloc] initWithData:data];
    [xmlParser setDelegate:self];
    [xmlParser setShouldProcessNamespaces:NO];
    [xmlParser setShouldReportNamespacePrefixes:NO];
    [xmlParser setShouldResolveExternalEntities:NO];
    [xmlParser parse];
   
}


# pragma mark View Connection Functionalities

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

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


-(void) parser:(NSXMLParser *) parser
didStartElement:(NSString *) elementName
  namespaceURI:(NSString *) namespaceURI
 qualifiedName:(NSString *) qName
    attributes:(NSDictionary *) attributeDict {
   
    //    CurrentElement = [elementName copy];
    //    NSUserDefaults *userdefaults=[NSUserDefaults standardUserDefaults];
   
    NSLog(@"Element name %@%@",elementName,soapResults);
   
    if( [elementName isEqualToString:@"NewDataSet"])      //enter the tag you want to start
    {
        if (!soapResults)
        {
            soapResults = [[NSMutableString alloc] init];
        }
        elementFound = true;
    }
   
}

-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
    if(elementFound)
    {
       
        soapResults = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSLog(@"soap results %@",soapResults);
    }
   
   
}

-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
 namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
   
    if([elementName isEqualToString:@""])
    {
        [arr1 addObject:soapResults];

    }
   
    if([elementName isEqualToString:@""])
    {
        [arr2 addObject:soapResults];
        }
   
    if([elementName isEqualToString:@""])
    {
        [arr3 addObject:soapResults];

    }
      
}


- (void)requestDone:(ASIHTTPRequest *)request{
   
    NSString *response = [request responseString];
   
    NSLog(@"%@",response);
   
    [self parseUser:response];
   
              
       
        [activity stopAnimating];
       
        activity.hidden=TRUE;
       
    }
   

- (void)requestWentWrong:(ASIHTTPRequest *)request

{
    NSError *error = [request error];
   
    NSLog(@"%@",error);
   
}


- (void)viewDidLoad
{
    [super viewDidLoad];
   

    [self connectionString];
   
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end