【发布时间】:2016-06-13 06:38:53
【问题描述】:
我正在尝试通过 Android 应用程序使用 Google Directions API。
在 google 开发者控制台上,我创建了一个项目并选择了 Android 平台。
使用我唯一的包名称和 SHA1 密钥,我得到了一个 API 密钥。
在我的 android 代码中,我使用此 API 密钥发送 HTTP 请求。
url = "https://maps.googleapis.com/maps/api/directions/json?origin=Dhanmondi&destination="+destination+"&key=" + apiKey;
public String sendGetRequest(String uri) {
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String result;
StringBuilder sb = new StringBuilder();
while((result = bufferedReader.readLine())!=null){
sb.append(result);
}
return sb.toString();
} catch (Exception e) {
return null;
}
}
但是,我收到了这个错误。
此 IP、站点或移动应用程序无权使用此 API 密钥。从 IP 地址 112.13.34.221 收到的请求,引用者为空。
我已启用 Google Maps for Android API 以及 Google Directions、Google Roads 和 Google Places API
【问题讨论】:
-
您是否在创建密钥的过程中添加了相同的包名,是否启用了路线 API,并在清单中添加了密钥
-
我有你提到的一切@war_Hero
-
返回的错误状态码是什么,如果您将 url 粘贴到浏览器选项卡中,请检查是否收到响应
-
状态码是 403 ...我已经编辑了我的问题,提供了屏幕截图 ... URL 在浏览器中不起作用,因为我在创建项目时选择了 android 平台@war_Hero
-
跟url解析有关
标签: android google-maps google-api google-directions-api