Search code examples
androidgoogle-distancematrix-api

Can i get all the data from google distance matrix api in one HTTP request?


I am using google distance matrix api to get the driving time between 2 coordinates.

This is my method for getting the data:

 private void testDistanceApi(String url) throws IOException {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url(url)
            .build();
    Response response = client.newCall(request).execute();
    System.out.println("this is my response --------" +response);
}

And this is the response that i am getting:

 System.out: this is my response --------Response{protocol=h2, code=200, message=, url=https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=32.002700,%2034.750875&destinations=31.963263,%2034.748292&key=AIzaSyAzFFPgX-GryNpheDClG-PpEr1OGuHm6OY}

In the response (JSON) there is the URL (as you can see in the print), I want to know If I can get the JSON from the URL without using another HTTP request.

The URL from the JSON:

url=https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=32.002700,%2034.750875&destinations=31.963263,%2034.748292&key=AIzaSyAzFFPgX-GryNpheDClG-PpEr1OGuHm6OY

Solution

  • I found the solution, I never parsed the response into JSON, there is no need for another HTTP request. I just parsed the response like this :

        String resStr = response.body().string();
        JSONObject json = new JSONObject(resStr);
        System.out.println("jsonnnnn????" + json);