Retrieving JSON Data Using NSURLSession

I am trying to get data from google distance api using NSURLSession, but as shown below, in the code, when I print the response and data, I get the results as NULL. What could be the problem? or is there another better way to get JSON data.

NSString *urlAsString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&key=API-KEY"]; NSURL *url = [NSURL URLWithString:urlAsString]; NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; [[session dataTaskWithURL:[NSURL URLWithString:urlAsString] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"RESPONSE: %@",response); NSLog(@"DATA: %@",data); }] resume]; 
+7
json ios objective-c iphone nsurlsession
source share
3 answers

You should use stringByAddingPercentEscapesUsingEncoding: in the url string, so you did not get the answer: the server returned an error.

You should have checked error ;)

I replaced your API key with a URL string, do not forget to leave your own if you copy / paste my code :)

 NSString *urlAsString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&key=YOUR-API-KEY"]; NSCharacterSet *set = [NSCharacterSet URLQueryAllowedCharacterSet]; NSString *encodedUrlAsString = [urlAsString stringByAddingPercentEncodingWithAllowedCharacters:set]; NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; [[session dataTaskWithURL:[NSURL URLWithString:encodedUrlAsString] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"RESPONSE: %@",response); NSLog(@"DATA: %@",data); if (!error) { // Success if ([response isKindOfClass:[NSHTTPURLResponse class]]) { NSError *jsonError; NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError]; if (jsonError) { // Error Parsing JSON } else { // Success Parsing JSON // Log NSDictionary response: NSLog(@"%@",jsonResponse); } } else { //Web server is returning an error } } else { // Fail NSLog(@"error : %@", error.description); } }] resume]; 
+7
source share

You can get some really good advice if you print out what was returned in the error parameter.

i.e:.

 NSString *unencodedURLString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&key=API-KEY"]; NSString *encodedURLString = (NSString *)CFURLCreateStringByAddingPercentEscapes( NULL, (CFStringRef)unencodedURLString, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 ); [[session dataTaskWithURL:[NSURL URLWithString:encodedURLString] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if (error != nil) { // if there an error, print it out... NSLog(@"error in NSURLSession is %@", [error localizedDescription]); } else { NSLog(@"RESPONSE: %@",response); NSLog(@"DATA: %@",data); } }] resume]; 

Below is the URL encoding procedure that I use.

+3
source share

From the documentation:

URL should be in the format:

https://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&key=YOUR_API_KEY

You are requesting:

 origins: Vancouver+BC|Seattle destinations: San+Francisco|Victoria+BC mode: driving key: API_KEY 

For transit:

https://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=transit&transit_mode=train&key=YOUR_API_KEY

You are requesting:

  origins: Vancouver+BC|Seattle destinations: San+Francisco|Victoria+BC mode: transit transit_mode: train key: API_KEY 
0
source share

All Articles