我正在尝试获取响应Google Directions API。您能找到此错误吗?
NSString *str = [NSString stringWithFormat:@"%@origin=%f,%f&destination=%f,%f&key=%@",@"https://maps.googleapis.com/maps/api/directions/json?",
self.marker.position.latitude,
self.marker.position.longitude,
self.SecondMarker.position.latitude,
self.SecondMarker.position.longitude,
@"AIzaSyDhZvxehd6ZDKFOB67WIyeLpy7KITwRPw0"];
//str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//NSString *params = @"";
[request setHTTPMethod:@"POST"];
//[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response
if (!error && [data length]) {
NSLog(@"Reponse: %@",response);
}
else{
NSLog(@"%@",error.description);
}
}] resume];
请建议一些代码行?
错误消息:
Reponse: <NSHTTPURLResponse: 0x60800002d480> { URL: https://maps.googleapis.com/maps/api/directions/json?origin=-33.860000,151.200000&destination=-34.224142,150.068408&key=AIzaSyDhZvxehd6ZDKFOB67WIyeLpy7KITwRPw0
} {状态代码:200,标题{ " cache-control" =" public,max-age = 86400"; "内容编码" = gzip; "内容长度" = 23856; " content-type" =" application/json; charset = utf-8"; date ="星期二,2017年4月25日08:48:36格林尼治标准时间"; Expires =" 2017年4月26日星期三08:48:36 GMT"; 服务器= mafe; vary ="接受语言"; " alt-svc" =" quic = ":443 "; ma = 2592000; v = " 37,36,35 ""; " X-Frame-Options" = Sameorigin; " X-XSS保护" =" 1; mode = block"; }}
尝试像这样
NSString *str = [NSString stringWithFormat:@"%@origin=%f,%f&destination=%f,%f&key=%@",@"https://maps.googleapis.com/maps/api/directions/json?",
self.marker.position.latitude,
self.marker.position.longitude,
self.SecondMarker.position.latitude,
self.SecondMarker.position.longitude,
@"AIzaSyDhZvxehd6ZDKFOB67WIyeLpy7KITwRPw0"];
str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
if (!error && [data length])
{
NSLog(@"Reponse: %@",response);
NSError *e = nil;
NSDictionary *JSONarray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:&e];
NSLog(@"Response dictionary is%@",JSONarray);
}
else
{
NSLog(@"%@",error.description);
}
}] resume];