2013-07-07 44 views
1

我有這個代碼使用,我試圖從我的Android設備調用Google方向API webservices:(4.1 HTC 1X)。但是,我收到了REQUEST_DENIED的響應。爲什麼?谷歌的方向API返回REQUEST_DENIED從Android中調用

這裏是我的代碼 - 沒有什麼花哨這裏:

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

我可能會做一些非常愚蠢的事情,但無法趕上它。

+0

有沒有什麼消息呢? – Vlad

+0

我使用map v2重寫了應用程序。現在它甚至沒有像以前那樣進步。 Web服務返回NULL。 XML選項已被禁用,我需要再次嘗試使用JSON – sunny

+0

..這次我將所有的Web服務調用作爲AsyncTask移動到後臺。看起來不推薦使用xml選項。我沒有試過JSON。由於其他工作承諾,永遠不要花時間去我的android代碼。任何人都可以使用XML工作。返回的XML DOM文檔是空的。它有子節點「方向響應」,但沒有低於它。 – sunny

回答

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; 
}