Home > database >  Google Maps Directions API blank response
Google Maps Directions API blank response

Time:02-09

I'm trying to get directions using the Google Maps Directions API.

It's pretty much what Google's own documentation shows to do https://developers.google.com/maps/documentation/directions/get-directions#maps_http_directions_toronto_montreal-java

The requested URL works when pasted into a browser, so that's correct.

And I can see an additional Directions API request after running this.

The response has no JSON directions in it.

What am I doing wrong?

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

    }

Image of 'response' in the debugger

CodePudding user response:

response.message() is not the body, it's just the string after the 200.

HTTP/1.1 200

check the response numerically or via if (!response.isSuccessful) {

This query is working for me.

{
  "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
        }
      },
  •  Tags:  
  • Related