0

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

If this has helped you, please mark it as your correct answer. Thanks
can you please xplain how you did that??
sorry I didn't get it, what do you want me to explain?
0

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);
}

1 Comment

@pankaj jha! Is the API KEY = 123456 which you are using in the url is it a valid key obtained from factual.com! because JSON Viewer is giving the following error!! The API key provided is invalid. To request an API key, please visit factual.com/devtools/beta."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.