我試圖從Android應用程序的Google Places中獲取信息。爲此,首先我在我的Google帳戶中啓用了此API。請求在Android應用程序中使用Google Places API Key for Server Web
第二,我已經創建了瀏覽器的API密鑰。由於另一個API,我已經有了一個API KEY服務器。
所以,在我的代碼我一直在測試這兩個鍵以及帶我有總是相同的結果!
{
「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金鑰,我已經得到了與兩個同樣的結果。
此致,我對這個問題感到絕望,因爲我不知道我做錯了什麼!
謝謝@Daniel紐金特!!!我已經啓用了我的谷歌地方PI Web服務和...仍然無法正常工作!我認爲你是對的,我必須使用Google Places API Web服務,也許我的代碼有問題。爲了進行測試,我使用了API密鑰服務器和API密鑰瀏覽器 –
請注意,該文檔說明您需要使用服務器密鑰,但我已將其與瀏覽器密鑰一起使用,它也起作用。以下是文檔:https://developers.google.com/places/webservice/intro –
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應用程序獲取信息,它不起作用! :( –