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

标签 android google-directions-api

我有这段代码,我正在尝试从我的 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/

相关文章:

android - 使用 Google Play 服务检查 GPS 状态

android - 尝试在 php 服务器中上传图片,但它可以发布。在安卓中

android - Ionic + Angular - 平台添加 Android 错误 - 错误 : Failed to fetch platform cordova-android@0. 0.8

android - 如何更改数组适配器中包含的 TextView 中的值

android - 标记来自 Google Maps Directions API 响应的高速公路

javascript - 用于在 Google Maps v3 上创建多条路线的数组

android - 如何强制对话的横向模式?

ios - 是否可以使用 iOS Google Maps SDK 检索路线的时间?

javascript - Google Maps Directions API - 单击按钮时显示备用路线

google-apps-script - 提供 API key 以避免 Apps 脚本中的 map 服务出现命中限制错误