0

In my current xcode project, I need to send parameter values to a url and need to retrieve an xml file based on the parameter values sent.

I tried the below code but it's not working:

(IBAction)myButtonClick:(id)sender 
{

    NSURL *oRequestURL = 
        [NSURL URLWithString:@"http://xyz .com/air/1.0/search?from=MAA&to=CJB&depart-date=2012-06-30&adults=2&children=2&infants=1&way=one&cabin-type=Economy&sort=asc"];

    NSMutableURLRequest *oRequest = [[[NSMutableURLRequest alloc]init]autorelease];

    [oRequest setHTTPMethod:@"POST"];
    [oRequest setURL: oRequestURL];

    NSMutableData *oHttpBody = [NSMutableData data];
    [oHttpBody appendData:[@"This is HTTP Request body" dataUsingEncoding:NSUTF8StringEncoding]];

    [oRequest setValue:[oHttpBody length] forHTTPHeaderField:@"Content-Length"];

    NSError *oError = [[NSError alloc]init];
    NSHTTPURLResponse *oResponseCode = nil;
    NSData *oResponseData = [NSURLConnection sendSynchronousRequest:oRequest returningResponse:oResponseCode error:oError];

    if ([oResponseCode statusCode]> 200) {
        NSLog(@"Status code is greater than 200");        
    }

    NSString *strResult=[[NSString alloc]initWithData:oResponseData encoding:NSUTF8StringEncoding];
    NSLog(@"The result is %s",strResult);

}

I have searched many sites and books but could not find a solution. Would be of great help if a link to a tutorial or some other useful resource can be provided. Appreciate your great help.

Thank You.

Hi, I have found the solution. The code is as below. Hope it helps someone else:)

- (IBAction)myButtonPressed:(id)sender 
{

    NSString *urlAsString = @"http://api.abc.com/air/1.0/search?from=MAA&to=CJB&depart-date=2012-09-30&adults=2&children=2&infants=1&way=one&cabin-type=Economy&sort=asc";


    NSURL *url = [NSURL URLWithString:urlAsString];


    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];


    [urlRequest setValue:@"2193141de2g7e2a34bb19bc3aa52b3b5" forHTTPHeaderField:@"X-XX-API-KEY"];

    [urlRequest setTimeoutInterval:30.0f];
    [urlRequest setHTTPMethod:@"GET"];


    NSOperationQueue *queue  = [[NSOperationQueue alloc]init];

    [NSURLConnection 
     sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
    {
         if ([data length]>0 &&
             error == nil)
         {
             NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
             NSLog(@"HTML = %@", html);
         }

         else if ([data length]== 0 && error==nil) {
             NSLog(@"Nothing was downloaded");
         }
     else if (error!= nil) {
         NSLog(@"Error occured = %@", error);
     }


     }];
}
9to5ios
  • 5,319
  • 2
  • 37
  • 65
user1583893
  • 9
  • 1
  • 3
  • I am not sure, are you looking for something like this? http://mentormate.com/blog/mapping-xml-document-object-model-dom-iphone-application/ – Vivek T Sep 05 '12 at 10:22
  • Hi Vivek, Thanks for the link. I need to Pass parameter to url like HttpGet request=new HttpGet("http://xyz.com/air/1.0/search?from=MAA&to=CJB&depart-date=2012-06-30&adults=2&children=2&infants=1&way=one&cabin-type=Economy&sort=asc"); Then the response will be an XML format. My request is how to do this in Xcode. A tutorial will be of great help. Thanks – user1583893 Sep 05 '12 at 11:54
  • "but it's not working" is not even close to enough information to help you here. It should go without saying, but, what's not working? Also if you want the equivalent of `HttpGet` then why are you setting the HTTP method to POST in your code? Also, you are not trying to do this in Xcode (Xcode is an IDE not a language) you are trying to do it in objective-c, that might help your searching. – NJones Sep 06 '12 at 06:24
  • Hi NJones,thanks for indicating I will follow it. Also I have found the way. I have edited above with the working code. Hope it helps someone else:) Thanks for all your support. – user1583893 Sep 06 '12 at 11:17

0 Answers0