2011-05-22 97 views
1

我試圖訪問谷歌使用代碼這樣的地方API從AppEngine上:嘗試訪問使用谷歌API客戶端谷歌地方GAE

字符串PLACES_DETAILS_URL =「https://maps.googleapis.com/maps/api /地點/信息/ JSON「;

 // setup up the HTTP transport 
    HttpTransport transport = new UrlFetchTransport(); 
    // add default headers 
    GoogleHeaders defaultHeaders = new GoogleHeaders(); 
    transport.defaultHeaders = defaultHeaders; 
    transport.defaultHeaders.put("Content-Type", "application/json"); 
    JsonHttpParser parser = new JsonHttpParser(); 
    parser.jsonFactory = new JacksonFactory(); 
    transport.addParser(parser); 

    // build the HTTP GET request and URL 
    HttpRequest request = transport.buildGetRequest(); 
    request.setUrl(PLACES_DETAILS_URL); 
    GenericData data = new GenericData(); 
    data.put("reference", restaurantGoogleId); 
    data.put("sensor", "false"); 
    data.put("key", ApplicationConstants.GoogleApiKey); 
    JsonHttpContent content = new JsonHttpContent(); 
    content.jsonFactory=new JacksonFactory(); 
    content.data = data; 
    request.content = content; 

    try { 
     HttpResponse response = request.execute(); 
     String r = response.parseAsString(); 

     r=r; 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

即使這是推薦的方式,我也不知道。如果是這樣,爲什麼這不起作用?如果我直接在瀏覽器中發送請求,它會起作用,但使用此代碼時,它總是會返回「拒絕請求」。

在此先感謝。

回答

0

在年底很容易,我混get和post動詞:

HttpTransport transport = new UrlFetchTransport(); 
     // add default headers 
     GoogleHeaders defaultHeaders = new GoogleHeaders(); 
     transport.defaultHeaders = defaultHeaders; 
     transport.defaultHeaders.put("Content-Type", "application/json"); 
     JsonCParser parser = new JsonCParser(); 
     parser.jsonFactory = new JacksonFactory(); 
     transport.addParser(parser); 

     // build the HTTP GET request and URL 
     HttpRequest request = transport.buildGetRequest(); 
     request.setUrl("https://maps.googleapis.com/maps/api/place/details/json?reference=CmRYAAAAciqGsTRX1mXRvuXSH2ErwW-jCINE1aLiwP64MCWDN5vkXvXoQGPKldMfmdGyqWSpm7BEYCgDm-iv7Kc2PF7QA7brMAwBbAcqMr5i1f4PwTpaovIZjysCEZTry8Ez30wpEhCNCXpynextCld2EBsDkRKsGhSLayuRyFsex6JA6NPh9dyupoTH3g&sensor=true&key=<APIKEY>"); 


     try { 
      HttpResponse response = request.execute(); 
      String r = response.parseAsString(); 
+0

u能告訴庫需要此代碼工作? – Vik 2012-06-12 00:14:43

相關問題