Akshar A K
Akshar A K

Reputation: 412

Webservices In Apple Iphone connection

Am having a webservice. Need my apple code to connect and pass 3parameters to the web service. Am very new to Objective C. Can anyone provide a sample code or guide me through this? Have searched links in StackOverflow. But not able to understand them.

Thanks In Advance.

Upvotes: 0

Views: 149

Answers (3)

Ravi Sharma
Ravi Sharma

Reputation: 975

You using soap message okey then akshar first you create a class of NSObject and then define a method in .h file with 3 parameter as given in below code:-

in .h file

@interface GetPartParser : NSObject <NSXMLParserDelegate>
{
    NSXMLParser *xmlParser;
    NSMutableArray *getPartArr;
    NSMutableDictionary *item;
    NSString *currentElement;
    NSMutableString *part_urlStr, *partNameStr;
}
@property (nonatomic,retain) NSMutableArray *getPartArr;
-(void) fetchPartXMLData:(NSInteger)empid:(NSInteger)serid:(NSInteger)seaid:(NSInteger)episodeid;

and in .m file :-

-(void) fetchPartXMLData:(NSInteger)empid:(NSInteger)serid:(NSInteger)seaid :(NSInteger)episodeid
{
    NSURL *myURL = [NSURL URLWithString:@"http://webservices.asmx"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: myURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    NSString *soapMessage = [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>"
                             "<GetPart xmlns=\"http://tempuri.org/\">"
                             "<empid>%d</empid>"
                             "<serid>%d</serid>"
                             "<seaid>%d</seaid>"
                             "<episodeId>%d</episodeId>"
                             "</GetPart>"
                             "</soap:Body>"
                             "</soap:Envelope>",empid,serid,seaid,episodeid];
    NSLog(@"soapMessage :%@",soapMessage);
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"http://tempuri.org/GetPart" forHTTPHeaderField:@"SOAPAction"];

    NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMessage length]];
    [request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPMethod: @"POST"];
    [request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLResponse *returnedResponse = nil;
    NSError *returnedError = nil;
    NSData *barData  = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnedResponse error:&returnedError];
    NSString *barString = [[NSString alloc] initWithBytes:[barData bytes] length:[barData length] encoding:NSUTF8StringEncoding];
    NSLog(@"barString :%@",[[barString stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"] stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"]);

    NSData* data=[[[barString stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"] stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"] dataUsingEncoding:NSUTF8StringEncoding];

    xmlParser = [[NSXMLParser alloc] initWithData:data];       
    [xmlParser setDelegate:self];
    [xmlParser setShouldResolveExternalEntities:NO];
    [xmlParser setShouldProcessNamespaces:NO];
    [xmlParser setShouldReportNamespacePrefixes:NO];
    [xmlParser parse];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    getPartArr =[[NSMutableArray alloc] init];
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed from web site (Error code %i )", [parseError code]];
    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    {
        currentElement = [elementName copy];
        if ([elementName isEqualToString:@"part"])
        {
            item = [[NSMutableDictionary alloc] init];
            part_urlStr = [[NSMutableString alloc]init];
            partNameStr = [[NSMutableString alloc]init];
        }
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if ([currentElement isEqualToString:@"part_url"])
    {
        [part_urlStr appendString:string];
    }
    if ([currentElement isEqualToString:@"partName"])
    {
        [partNameStr appendString:string];
    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"part"])
    {
        [item setObject:part_urlStr forKey:@"part_url"];
        [item setObject:partNameStr forKey:@"partName"];
        [getPartArr addObject:[item copy]];
    }
    NSLog(@"item dictionary....%@",partNameStr);
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{    
    NSLog(@"getPartArr :%@",getPartArr);

}

it helps you and give what you want... if you like then mark it as accepted

Upvotes: 0

GeT
GeT

Reputation: 550

An event simpler way to get a ws result is:

NSString *urlString= [NSString stringWithFormat:@"http://demo.xyz.com/proj/webservices/display_records/1/%@%@%@",myid,name,password];
NSString *wsRes = [NSString stringWithContentsOfURL:urlStribng encoding: NSUTF8StringEncoding error:nil];

Consider extending this sample with error handling if you are going to use it.

Upvotes: 0

Sanket Pandya
Sanket Pandya

Reputation: 1095

   NSString *urlString= [NSString stringWithFormat:@"http://demo.xyz.com/proj/webservices/display_records/1/%@%@%@",myid,name,password];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
conn= [[NSURLConnection alloc]initWithRequest:request delegate:self];

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

Use defalut connection methods

Upvotes: 1

Related Questions