我正在尝试从Android应用的Google商家信息API获取信息。为此,我首先在Google帐户中启用了此API。
其次,我已经为浏览器创建了一个API KEY。由于其他API,我已经有了一个API KEY服务器。
所以,在我的代码中,我已经使用这两个密钥进行了测试,并且两者都得到了相同的结果!
{
“error_message”:“此服务需要API密钥。”,
“html_attributions”:[],
“结果”:[],
“status”:“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();
}
当我调用API时,我得到了ResponseCode = 200,而我构建的调用最终就是那样......
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 Key和浏览器应用程序的Api Key,而且两者都得到了相同的结果。
真诚地,我对这个问题感到绝望,因为我不知道我做错了什么!!!
答案 0 :(得分:6)
问题是您没有使用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
,它将起作用:
如果您在登录Google云端控制台帐户时转到此链接: https://console.cloud.google.com/apis/library?filter=category:maps
这是应该启用的API:
答案 1 :(得分:1)
来自文档:
注意:您需要Android API密钥,而不是浏览器密钥。您可以为Google Maps Android API v2应用和适用于Android应用的Google Places API使用相同的API密钥。
检查this以获取更多帮助。
答案 2 :(得分:0)
更简单的方法是尝试最新的GCM配置文件实现,并使用他们的开发人员界面轻松创建项目。