【问题标题】:Google direction API returns REQUEST_DENIED when called from Android从 Android 调用时,Google 方向 API 返回 REQUEST_DENIED
【发布时间】:2013-07-04 17:24:56
【问题描述】:

我有这段代码,我试图从我的 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 的密钥。但是,我没有使用最新的 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

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

【问题讨论】:

  • 我使用地图 v2 重新编写了应用程序。现在它甚至没有像以前那样进步。 Web 服务返回 NULL。是否禁用了 XML 选项,我需要使用 JSON
  • 再次尝试.. 这次我将所有 Web 服务调用作为 AsyncTask 移至后台。看起来不推荐使用 xml 选项。我没有尝试 JSON。由于其他工作承诺,永远没有时间访问我的 android 代码。任何人都可以使用 XML 来完成这项工作。返回的 xml dom 文档为空。它有子节点“Direction Response”,但下面没有。
  • 就个人而言,我更喜欢 JSON 而不是 XML,所以我从未尝试过 XML。我有一个 REQUEST_DENIED 问题,我通过从请求中完全删除 API 密钥来解决这个问题。但这只是一个临时修复,因为它只会申请一个免费的 API 帐户(我想,我对所有这些 API 密钥有点迷茫。我不明白为什么我需要来自不同平台的不同密钥相同的帐户..但哦,好吧..)
  • 谢谢弗拉德 - 至少我有你分享我的烦恼。我的请求没有任何 API 密钥。谷歌文档不推荐使用一个。我正在为 Google Map v2 使用 API 密钥。据我了解,方向 API 是免费的。我看到的问题是,在调试模式下运行它时,后台进程不断调用谷歌网络服务多次。第一次它空手而归,第二次它得到了一些东西,但除了“方向响应”之外什么也没有。但是要注意的是,进行了多次调用。另外,弗拉德,它对你有用 JSON 吗?。
  • @Vlad,仍然收到“请求被拒绝”..

标签: android google-directions-api


【解决方案1】:

发布对我有用的代码。我正在使用 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 2013-06-17
    • 2013-12-01
    • 2012-01-15
    • 2012-02-22
    相关资源
    最近更新 更多