Ashutosh
Ashutosh

Reputation: 5742

How to pass a json object as a parameter to the webservice?

I am using a webservice which takes json object as parameter. Here's my code:

    -(void)createHttpHeaderRequest {

        NSString *x = @"{\"GetVehicleInventory\": {\"ApplicationArea\": {\"Sender\": {\"ComponentID\":}}}" (something like that)

        NSString *sample = [NSString stringWithFormat:@"https://trialservice.checkitout?XML_INPUT=%@",x];
 NSString * final = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)sampleReq, NULL, CFSTR(":/?#[]@!$&'()*+,;=\""), kCFStringEncodingUTF8);
 NSMutableRequest *request = [NSMutableREquest requestWithURL:[NSURL URLWithString:final]];
    NSURLConnection * theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
        if (theConnection) {
            NSLog(@"Service hit");
        }
    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        NSError * error;
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        NSArray * categories = [dict objectForKey:@"category"];
        [self.delegate giveControlBackToController:categories];

    }

When I am trying to NSLog sample it gives me the complete URL which on pasting into a browser gives me the result back, but when I am calling NSLog on the request, it shows null and nothing happens after this. The control never goes to its NSURLConnection delegate method.

Upvotes: 0

Views: 293

Answers (2)

Ethan Holshouser
Ethan Holshouser

Reputation: 1401

This should probably be a comment instead of an answer, but unfortunately you can't format code in a comment. Add the following method to your delegate and see whether it gets called. If it does, let us know what the response is.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSLog(@"Connection response code: %d", httpResponse.statusCode);
}

Upvotes: 0

user529758
user529758

Reputation:

The trick is that most browsers automatically escape URLs whilst NSURL doesn't. You'll need to do it manually; have a look at the CFURLCreateStringByAddingPercentEscapes function.

Upvotes: 1

Related Questions