2015-06-23 69 views
3

我試圖從Android應用程序的Google Places中獲取信息。爲此,首先我在我的Google帳戶中啓用了此API。請求在Android應用程序中使用Google Places API Key for Server Web

enter image description here

enter image description here

第二,我已經創建了瀏覽器的API密鑰。由於另一個API,我已經有了一個API KEY服務器。

enter image description here

所以,在我的代碼我一直在測試這兩個鍵以及帶我有總是相同的結果!

{

「ERROR_MESSAGE」: 「此服務需要一個API密鑰。」,

「html_attributions」:[],

「的結果」:[],

「狀態」: 「REQUEST_DENIED」

}

,我使用撥打電話,在代碼...

 @Override 
    protected String doInBackground(LocationService... ls) { 
     JSONObject result = new JSONObject(); 
     URL url; 
     HttpsURLConnection urlConnection; 

     // Making HTTP request 
     try { 

      //Define connection 
      url = new URL("https://maps.googleapis.com/maps/api/place/nearbysearch/json"); 
      urlConnection = (HttpsURLConnection)url.openConnection(); 

      urlConnection.setRequestMethod("POST"); 
      urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      urlConnection.setRequestProperty("charset", "utf-8"); 
      urlConnection.setRequestProperty("Accept", "application/json"); 
      urlConnection.setDoOutput(true); 
      urlConnection.setDoInput(true); 
      urlConnection.setUseCaches(false); 

      //Send data 
      String parameters = "?location=" + String.valueOf(ls[0].getLocation().getLatitude()) + "," + String.valueOf(ls[0].getLocation().getLongitude()); 
      parameters+="&radius=5000"; 
      parameters+="&types=restaurant|health|city_hall|gas_station|shopping_mall|grocery_or_supermarket"; 
      parameters+="&sensor=false"; 
      parameters+="&key=" + Constants.API_KEY_BROWSER_APPLICATIONS; 
      byte[] postData = parameters.getBytes(Charset.forName("UTF-8")); 
      int postDataLength = postData.length; 
      urlConnection.setRequestProperty("Content-Length", Integer.toString(postDataLength)); 
      DataOutputStream data = new DataOutputStream(urlConnection.getOutputStream()); 
      data.write(postData); 
      data.flush(); 
      data.close(); 

      Log.d(TAG, "Datos enviados"); 
      Log.d(TAG, "ResponseCode: " + String.valueOf(urlConnection.getResponseCode())); 

      //Display what returns POST request 

      StringBuilder sb = new StringBuilder(); 

      int HttpResult = urlConnection.getResponseCode(); 

      if(HttpResult == HttpURLConnection.HTTP_OK){ 
       String json; 

       BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8")); 

       String line; 

       while ((line = br.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 

       br.close(); 

       //System.out.println(""+sb.toString()); 
       Log.d(TAG, "json: " + sb.toString()); 

       FileService file = new FileService(); 
       file.writeLog(POIActivity.TAG, getClass().getName(), POIActivity.urlConnection + parameters); 
       file.writeLog(POIActivity.TAG, "doInBackground", sb.toString()); 

       // Parse the String to a JSON Object 
       result = new JSONObject(sb.toString()); 

      }else{ 
       //System.out.println(urlConnection.getResponseMessage()); 
       Log.d(TAG, "urlConnection.getResponseMessage(): " + urlConnection.getResponseMessage()); 
       result = null; 
      } 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
      Log.d(TAG, "UnsuppoertedEncodingException: " + e.toString()); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      Log.d(TAG, "Error JSONException: " + e.toString()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      Log.d(TAG, "IOException: " + e.toString()); 
     } 

     // Return JSON Object 
     return result.toString(); 


    } 

當我打這個電話給我有一個像ResponseCode = 200的API和呼叫我打造的是最後這樣的...

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=38.26790166666667,-0.7052183333333333&radius=5000&types=restaurant|health|city_hall|gas_station|shopping_mall|grocery_or_supermarket&sensor=false&key=API_KEY 

記住,就像我API_KEY都用了,阿比主要用於服務器應用程序和瀏覽器應用程序API金鑰,我已經得到了與兩個同樣的結果。

此致,我對這個問題感到絕望,因爲我不知道我做錯了什麼!

回答

5

的問題是,你沒有使用Google Places API for Android,您使用的是Google Places API Web Service

Here是使用Google Places API for Android的示例,並且here是使用Google Places API Web Service的示例。你肯定使用後者。

啓用Google Places API Web Service,它會工作:

如果你去這個鏈接。登錄谷歌雲端控制檯帳戶: https://console.cloud.google.com/apis/library?filter=category:maps

這是應該啓用API:

enter image description here

+0

謝謝@Daniel紐金特!!!我已經啓用了我的谷歌地方PI Web服務和...仍然無法正常工作!我認爲你是對的,我必須使用Google Places API Web服務,也許我的代碼有問題。爲了進行測試,我使用了API密鑰服務器和API密鑰瀏覽器 –

+1

請注意,該文檔說明您需要使用服務器密鑰,但我已將其與瀏覽器密鑰一起使用,它也起作用。以下是文檔:https://developers.google.com/places/webservice/intro –

+0

Yeees !!!我都推出了這個網址https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=38.26790166666667,-0.7052183333333333&radius=5000&types=restaurant|health|city_hall|gas_station|shopping_mall|grocery_or_supermarket&sensor=false&key=API_KEY_SERVER與我的API KEY服務器在我的瀏覽器上,它的工作原理!但是,如果我試圖通過我的Android應用程序獲取信息,它不起作用! :( –

1

從文檔:

注意:您需要一個Android API密鑰,而不是瀏覽器的關鍵。您可以爲您的Google Maps Android API v2應用和Google Places API for Android應用使用相同的API密鑰。

檢查this尋求更多幫助。

+0

謝謝,但......我只是做了Android API密鑰的測試,並且不工作:( –

相關問題