我试图使用NSURLSession从google distance api获取数据,但是当我打印响应和数据时,如下所示,我得到的结果为NULL。 可能是什么问题?或者是否有任何其他更好的方法来获取JSON数据。
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];
答案 0 :(得分:8)
您应该在您的网址字符串上使用stringByAddingPercentEscapesUsingEncoding:
,这就是您没有收到回复的原因:服务器返回了错误。
您应该检查error
;)
我在网址字符串中替换了您的API密钥,如果您复制/粘贴我的代码,请记得自己放置:)
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];
答案 1 :(得分:3)
如果你打印出错误参数中返回的内容,你可能会得到一个非常好的提示。
即:
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's an error, print it out...
NSLog(@"error in NSURLSession is %@", [error localizedDescription]);
} else {
NSLog(@"RESPONSE: %@",response);
NSLog(@"DATA: %@",data);
}
}] resume];
网址编码例程我使用is found here。
答案 2 :(得分:0)
来自文档:
网址必须采用以下格式:
您正在请求:
origins: Vancouver+BC|Seattle
destinations: San+Francisco|Victoria+BC
mode: driving
key: API_KEY
For Transit:
您被要求:
origins: Vancouver+BC|Seattle
destinations: San+Francisco|Victoria+BC
mode: transit
transit_mode: train
key: API_KEY