1

このコードを使用して、Android デバイスから Google 方向 API Web サービスを呼び出そうとしています: (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 は使用する必要がないと考えているため、使用していません。私は完全に無知です。4.1 を使用する HTC 1x で実行しながら、Android 3.2 を使用してアプリを作成しています。

ブラウザから使用する場合も同様です。たとえば、任意のブラウザーから呼び出された場合、次のように動作します (ただし、私の Android コードからではありません): http://maps.googleapis.com/maps/api/directions/xml?origin=37.2345487,-121.5840723&destination=37.236064,- 121.961595&センサー=false

私は非常に愚かなことをしているかもしれませんが、それを捕まえることはできません。

4

1 に答える 1

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;
}
于 2013-09-08T14:13:42.907 に答える