gpt4 book ai didi

android - 从 Android 调用 Google 方向 API 时返回 REQUEST_DENIED

转载 作者:行者123 更新时间:2023-11-30 03:30:50 24 4
gpt4 key购买 nike

我有这段代码,我正在尝试从我的 Android 设备调用 Google 方向 API 网络服务:(4.1 HTC 1X)。但是我得到了响应 REQUEST_DENIED。为什么?

这是我的代码 - 这里没什么特别的:

String hosturl="https://maps.googleapis.com/maps/api/directions/xml?";
String url = "origin=" +lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=true";
String tag[] = { "lat", "lng" };

ArrayList<GeoPoint> list_of_geopoints = new ArrayList<GeoPoint>();
HttpResponse response = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
String newurl = URLEncoder.encode(url, "UTF-8");
String finalurl = hosturl + newurl;
System.out.println(" Direction request going out:"+ finalurl);
HttpPost httpPost = new HttpPost(finalurl);
try {
response = httpClient.execute(httpPost, localContext);
}
catch (Exception exc) {
System.out.println("IO Exeception in making direction request: "+ exc.getMessage());
//list_of_geopoints=null;
return list_of_geopoints;
}
InputStream in = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in);
if (doc != null) {
NodeList nl1, nl2, statusList;
statusList = doc.getElementsByTagName("status");

当我打印状态节点时,我得到一个 REQUEST_DENIED。

我看到其他人也发了同样的帖子,但没有得到任何回应。

我已经有了我的 MAP API 的 key 。但是,我没有使用最新的 Google Places API,因为我认为我不需要。我完全没有头绪。我正在使用 Android 3.2 制作该应用程序,同时在使用 4.1 的 HTC 1x 上运行它。

当从浏览器使用时同样有效。例如,以下从任何浏览器调用时都可以工作(但不是从我的 android 代码): http://maps.googleapis.com/maps/api/directions/xml?origin=37.2345487,-121.5840723&destination=37.236064,-121.961595&sensor=false

我可能正在做一些非常愚蠢的事情,但无法捕捉到它。

最佳答案

发布对我有用的代码。我正在使用 JSON,这只是一个 POC 脏代码,所以,如果它很难看,我很抱歉。希望这会有所帮助。

    public List<LatLng> getDirections(LatLng start, LatLng end, String mode){
try{
HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
HttpRequest request = httpRequestFactory.buildGetRequest(new GenericUrl(DIRECTIONS_URL));
request.getUrl().put("origin", start.latitude + "," + start.longitude);
request.getUrl().put("destination", end.latitude + "," + end.longitude);
request.getUrl().put("sensor", "true");
request.getUrl().put("mode", mode);

JSONObject obj = new JSONObject(request.execute().parseAsString());
JSONArray routes = obj.getJSONArray("routes");
JSONObject route = routes.getJSONObject(0);
JSONArray legs = route.getJSONArray("legs");
JSONObject leg = legs.getJSONObject(0);
JSONArray steps = leg.getJSONArray("steps");

List<LatLng> list = new ArrayList<LatLng>();

list.add(new LatLng(start.latitude, start.longitude));

for(int i=0;i<steps.length(); i++){
JSONObject step = steps.getJSONObject(i);
JSONObject loc = step.getJSONObject("end_location");
LatLng ll = new LatLng(loc.getDouble("lat"), loc.getDouble("lng"));
list.add(ll);
}

list.add(new LatLng(end.latitude, end.longitude));

return list;

} catch (HttpResponseException e) {
Log.e("Error:", e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

关于android - 从 Android 调用 Google 方向 API 时返回 REQUEST_DENIED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17508684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com