Reputation: 65
I have to parse the return of a JSON API for which I've taken the URL into a string and after that I passed it NSURL but in NSLog it is showing nil. Here is my code,
NSString *urlstring=[NSString stringWithFormat:@"http://api.v3.factual.com/t/places?q=%@&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[self.strurl lowercaseString]];
NSURL *url=[[NSURL alloc]initWithString:urlstring ];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
Thanks in advance.
Upvotes: 0
Views: 528
Reputation: 765
Try this, Tested and Working
-(void) sample
{
// NSString *strurl = @"hello";
NSString *urlstring=[NSString stringWithFormat:@"http://api.v3.factual.com/t/places?q=%@&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[strurl lowercaseString]];
NSURL *url=[NSURL URLWithString:[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSLog(@"%@",req);
}
Upvotes: 0
Reputation: 2176
You must encode your url as it contains special characters, Try something like this
NSString *paramString=[NSString stringWithFormat:@"/t/places?q=%@&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[self.strurl lowercaseString]];
NSString *encodedSearchString = [paramString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:@"http://api.v3.factual.com%@", encodedSearchString];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(@"urlstring is %@",url)
Hope this helps
Upvotes: 1