-1
\$\begingroup\$
public final class DistanceCalculator {

    public static void main(String args[]) {
        System.out.println("distance="
            + calculateDistance(
                "Khreschatyk Street 1, Kyiv, Kyiv city",
                "Peremohy Avenue 22, Kyiv, Kyiv city"
            )
        ); //distance=6266
        System.out.println("distance="
            + calculateDistance("50.45,31","50,30")
        );//distance=115384
    }

    public static int calculateDistance(String a, String b) {
        String url = null;
        String json = null;

        try {
            url = "http://maps.googleapis.com/maps/api/directions/json?origin="
                + URLEncoder.encode(a, "UTF-8") + "&destination="
                + URLEncoder.encode(b, "UTF-8") + "&region=ua&sensor=false";
        } catch (UnsupportedEncodingException e2) {
            System.out.println("something strange with encoding");
            e2.printStackTrace();
        }

        try {
            json = getJson(url);
        } catch (Exception e1) {
            System.out.println("something strange with your json");
            e1.printStackTrace();
        }

        JSONParser parser = new JSONParser();

        Object obj = null;
        try {
            obj = parser.parse(json);
        } catch (ParseException e) {
            System.out.println("error with parsing");
            e.printStackTrace();
        }
        JSONObject jsonObj = (JSONObject) obj;
        JSONArray ja = (JSONArray) jsonObj.get("routes");
        jsonObj = (JSONObject) ja.get(0);
        ja = (JSONArray) jsonObj.get("legs");
        jsonObj = (JSONObject) ja.get(0);
        jsonObj = (JSONObject) jsonObj.get("distance");
        return Integer.parseInt(jsonObj.get("value").toString());
    }

    public static String getJson(String pageAddress) throws Exception {
        StringBuilder sb = new StringBuilder();
        URL pageURL = new URL(pageAddress);
        URLConnection uc = pageURL.openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader(
            uc.getInputStream()));
        try {
            String inputLine;
            while ((inputLine = br.readLine()) != null) {
                sb.append(inputLine);
            }
        } finally {
            br.close();
        }
        return sb.toString();
    }

}
\$\endgroup\$

closed as unclear what you're asking by Jamal Dec 13 '16 at 6:49

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Your address string is malformed. Also, use Ctrl+K to indent everything by four spaces so that it is treated as a code block. \$\endgroup\$ – 200_success Apr 27 '15 at 8:29
  • 2
    \$\begingroup\$ What's the question? \$\endgroup\$ – segarci Apr 27 '15 at 8:35
2
\$\begingroup\$
    try {
        obj = parser.parse(json);
    } catch (ParseException e) {
        System.out.println("error with parsing");
        e.printStackTrace();
    }

Catching a possible exception and being specific about the exception type: Good.

Error handling solely consisting of printing an error message and stacktrace, but nothing else: Not good.

Using obj in the following context even though it might be still null since the error handler might have kicked in before the assignment happened: ...

The same problem occurs with all other attempts of error handling as well - you are consuming the exception, printing an error message - and then you are just continuing like nothing ever happened, possibly causing an null pointer exception in at least one location.

Either you do proper error handling, or you don't. In this case either rethrow the exception, don't catch it at all, or throw an generalized exception which you declare in the function signature.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.