从Android调用时,Google方向API会返回REQUEST_DENIED

时间:2013-07-07 02:13:31

标签: android google-direction

我有这个代码,我试图从我的Android设备调用Google Directions API webservices:(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

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

1 个答案:

答案 0 :(得分: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;
}