2017-04-25 3 views
0

Google Directions API에 대한 응답을 받으려고합니다. 이 오류를 찾으실 수 있습니까?길 찾기 API Google지도에서 어떻게 응답을 받으시겠습니까?

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, 헤더 { "캐시 제어"= "공공, 최대 연령 = 86400"; "Content-Encoding"= gzip; "Content-Length"= 23856; "Content-Type"= "application/json; charset = UTF-8"; 날짜 = "Tue, 25 Apr 2017 08:48:36 GMT"; 만료 = "Wed, 26 Apr 2017 08:48:36 GMT"; 서버 = mafe; Vary = "수락 언어"; "alt-svc"= "quic = \": 443 \ "; ma = 2592000; v = \"37,36,35 \ ""; "x-frame-options"= SAMEORIGIN; "x-xss-protection"= "1; mode = block"; }}

+0

같이하십시오. – KKRocks

+0

편집을 확인하십시오 –

+0

@HarjotSinghPanesar 이제 응답을 얻는 대신 오류가 발생하지 않습니다. 데이터와 직렬화 된 JSON에 액세스해야합니다. –

답변

0

하여 오류 메시지를 추가이

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]; 
관련 문제