1

I have this code using which I am trying to call the Google directions API webservices from my Android device: (4.1 HTC 1X). However I get the response REQUEST_DENIED. Why?

Here's my code - nothing fancy here:

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");

When I print the status node, I get a REQUEST_DENIED.

I see that others have also posted the same but did not get any response.

I already have a key for my MAP API. However, I am not using the latest Google Places API as I believe I do not need to. I am completely clueless. I am making the app using Android 3.2 while running it on my HTC 1x which uses 4.1.

The same works well when used from the browser. For example, the following when invoked from any browser would work ( but not from my android code): http://maps.googleapis.com/maps/api/directions/xml?origin=37.2345487,-121.5840723&destination=37.236064,-121.961595&sensor=false

I may be doing something very stupid but cannot catch it.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
sunny
  • 643
  • 2
  • 11
  • 29
  • I re-wrote the application using map v2. Now it doesn't even progress like before. web service returns NULL. Has the XML option been disabled and I need to use JSON – sunny Aug 22 '13 at 04:17
  • tried again.. this time I moved all the web service call to background as AsyncTask. Looks like the xml option is not recommended. I did not try JSON. Never get time to get to my android code due to other work commitments . anyone got this working using XML. The xml dom document returned is empty. it has the child node " Direction Response" but nothing below it. – sunny Sep 03 '13 at 03:06
  • Personally, i prefer JSON over XML so i've never tried XML. I had a REQUEST_DENIED issue, which i fixed by completely removing the API key from the request. but it's just a temporary fix, because it will only apply for a free API account (I think, i'm a little bit lost with all those API keys. I don't understand why do i need different keys from different platforms for the same account.. but oh well..) – Vlad Sep 03 '13 at 16:04
  • Thanks Vlad - atleast I have you to share my troubles. My request does not have any API keys. the google docs do not recommend using one. I am using API key for Google Map v2. From what I understood , direction APIs are free. The issue I see is that on running it on debug mode, the background process keeps on calling the google web service multiple times. the first time it returns empty handed , second time it gets something back but nothing beyond "Direction Response". However the point to note is that , multiple calls are made. Also, Vlad, did it work for you with JSON?. – sunny Sep 04 '13 at 02:14
  • @Vlad, still getting "REQUEST DENIED" .. – sunny Sep 04 '13 at 02:36
  • Please see my answer below for the code snippet i'm using. – Vlad Sep 08 '13 at 14:14

1 Answers1

1

Posting the code that worked for me. I'm using JSON, and this is just a POC dirty code, so, i'm sorry if it's ugly. Hope this helps.

    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;
}
Vlad
  • 735
  • 2
  • 10
  • 19