0

the following jsp file presents a code that gives users the distance between two countries using the google maps service, but the OkHttpClient / Request / JSONObject classes are not resolved to a type

I tried to import the classes but the problem is not solved .

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@import com.squareup.okhttp.OkHttpClient; %>
<%@import okhttp3.OkHttpClient; %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Distance Calculator</title>
</head>
<body>
    <h1>Distance Calculator</h1>
    
    <form action="" method="post">
        <label for="origin">Origin:</label>
        <input type="text" id="origin" name="origin" required><br><br>
        
        <label for="destination">Destination:</label>
        <input type="text" id="destination" name="destination" required><br><br>
        
        <input type="submit" value="Calculate Distance">
    </form>
    <% 
        if (request.getMethod().equals("POST")) {
            String apiKey = "YOUR_API_KEY";
            String origin = request.getParameter("origin").replaceAll("\\s", "+");
            String destination = request.getParameter("destination").replaceAll("\\s", "+");
            String url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins="
                    + origin + "&destinations=" + destination + "&units=metric&key=" + apiKey;
            
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            
            Response response = client.newCall(request).execute();
            String jsonData = response.body().string();
            
            JSONObject jsonObj = new JSONObject(jsonData);
            JSONArray rows = jsonObj.getJSONArray("rows");
            JSONObject elements = rows.getJSONObject(0).getJSONArray("elements").getJSONObject(0);
            String distance = elements.getJSONObject("distance").getString("text");
            String duration = elements.getJSONObject("duration").getString("text");
            
            out.print("<p>The distance between " + origin.replaceAll("\\+", " ")
                    + " and " + destination.replaceAll("\\+", " ") 
                    + " is " + distance + " and it will take about " + duration + " to get there.</p>");
        }
    %>
</body>
</html>

0 Answers0