簡體   English   中英

谷歌地圖方向 API 空白回復

[英]Google Maps Directions API blank response

我正在嘗試使用 Google 地圖路線 API 獲取路線。

這幾乎是谷歌自己的文檔顯示要做的事情https://developers.google.com/maps/documentation/directions/get-directions#maps_http_directions_toronto_montreal-java

請求的 URL 在粘貼到瀏覽器中時有效,所以這是正確的。

運行此命令后,我可以看到一個額外的 Directions API 請求。

響應中沒有 JSON 指示。

我究竟做錯了什么?

private class getDirections extends AsyncTask<LatLng, Void, String> {
        @Override
        protected String doInBackground(LatLng... point) {
            try {
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .writeTimeout(60, TimeUnit.SECONDS)
                        .readTimeout(60, TimeUnit.SECONDS)
                        .build();
                Request request = new Request.Builder()
                        .url("https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key="+MAPS_API_KEY)
                        .method("GET", null)
                        .build();
                    Response response = client.newCall(request).execute();
                    Log.d("response1", response.message());
                    if (response.message() == "") {
                        return "error";
                    }
                    return response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
        }

    }

調試器中“響應”的圖像

response.message()不是主體,它只是 200 之后的字符串。

HTTP/1.1 200

以數字方式或通過if (.response.isSuccessful) {檢查響應

這個查詢對我有用。

{
  "geocoded_waypoints": [
    {
      "geocoder_status": "OK",
      "place_id": "ChIJpTvG15DL1IkRd8S0KlBVNTI",
      "types": [
        "locality",
        "political"
      ]
    },
    {
      "geocoder_status": "OK",
      "place_id": "ChIJDbdkHFQayUwR7-8fITgxTmU",
      "types": [
        "locality",
        "political"
      ]
    }
  ],
  "routes": [
    {
      "bounds": {
        "northeast": {
          "lat": 45.5017123,
          "lng": -73.5672184
        },
        "southwest": {
          "lat": 43.6533961,
          "lng": -79.3834913
        }
      },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM