Google direction API从Android调用时返回拒绝的请求

Google direction API从Android调用时返回拒绝的请求,android,google-directions-api,Android,Google Directions Api,我有这段代码,我正试图使用它从我的Android设备(4.1 HTC 1X)调用Google directions API Web服务。但是,我收到的响应请求被拒绝。为什么? 这是我的代码-这里没有什么特别之处: String hosturl="https://maps.googleapis.com/maps/api/directions/xml?"; String url = "origin=" +lat1 + "," + lon1 + "&destination=" + lat2

我有这段代码,我正试图使用它从我的Android设备(4.1 HTC 1X)调用Google directions API Web服务。但是,我收到的响应请求被拒绝。为什么?

这是我的代码-这里没有什么特别之处:

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");
String hosturl=”https://maps.googleapis.com/maps/api/directions/xml?";
字符串url=“origin=“+lat1+”、“+lon1+”和destination=“+lat2+”、“+lon2+”&sensor=true”;
字符串标记[]={“lat”,“lng”};
ArrayList list_of_geopoints=新的ArrayList();
HttpResponse响应=null;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpContext localContext=新的BasicHttpContext();
字符串newurl=URLEncoder.encode(url,“UTF-8”);
字符串finalurl=hosturl+newurl;
System.out.println(“发出的指令请求:+finalurl”);
HttpPost HttpPost=新的HttpPost(最终);
试一试{
response=httpClient.execute(httpPost,localContext);
}
捕获(异常exc){
System.out.println(“发出方向请求时的IO异常:”+exc.getMessage());
//地质点列表=空;
返回地质点的列表;
}
InputStream in=response.getEntity().getContent();
DocumentBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
文档doc=builder.parse(in);
如果(doc!=null){
节点列表nl1、nl2、状态列表;
statusList=doc.getElementsByTagName(“状态”);
当我打印状态节点时,我得到一个请求被拒绝

我看到其他人也发布了同样的消息,但没有得到任何回应

我已经有了地图API的密钥。但是,我没有使用最新的Google Places API,因为我相信我不需要。我完全不知道。我使用Android 3.2制作应用程序,同时在使用4.1的HTC 1x上运行它

当从浏览器中使用时,同样适用。例如,当从任何浏览器调用时(但不是从我的android代码中),以下内容都可以工作:


我可能做了一些非常愚蠢的事情,但却没能抓住它。

发布了对我有用的代码。我使用的是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;
}
public List getDirections(LatLng开始、LatLng结束、字符串模式){
试一试{
HttpRequestFactory HttpRequestFactory=createRequestFactory(HTTP_传输);
HttpRequest请求=httpRequestFactory.buildGetRequest(新的GenericUrl(方向URL));
request.getUrl().put(“origin”,start.latitude+,“+start.longitude”);
request.getUrl().put(“destination”,end.latitude+,“+end.longitude”);
request.getUrl().put(“传感器”、“真”);
request.getUrl().put(“mode”,mode);
JSONObject obj=新的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”);
列表=新的ArrayList();
添加(新板条(起点.纬度,起点.经度));

for(int i=0;i发布了对我有用的代码。我使用的是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;
}
public List getDirections(LatLng开始、LatLng结束、字符串模式){
试一试{
HttpRequestFactory HttpRequestFactory=createRequestFactory(HTTP_传输);
HttpRequest请求=httpRequestFactory.buildGetRequest(新的GenericUrl(方向URL));
request.getUrl().put(“origin”,start.latitude+,“+start.longitude”);
request.getUrl().put(“destination”,end.latitude+,“+end.longitude”);
request.getUrl().put(“传感器”、“真”);
request.getUrl().put(“mode”,mode);
JSONObject obj=新的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”);
列表=新的ArrayList();
添加(新板条(起点.纬度,起点.经度));

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