Source:
private Location resolvePlaceLocationYandex(String nameOfLocation) throws InvalidLocationException, LocationDefinitionErrorException { int numberOfTries = 0; do { URL yandexMapsURL = null; try { try { URI uri = new URI("https", "geocode-maps.yandex.ru", "/1.x/", "geocode=" + nameOfLocation + "&key=" + yandexMapsKey + "&format=json", null); yandexMapsURL = uri.toURL(); } catch (URISyntaxException e) { e.printStackTrace(); } } catch (MalformedURLException e) { System.err.println(e.getMessage()); ++numberOfTries; continue; } try { assert yandexMapsURL != null; BufferedReader in = new BufferedReader(new InputStreamReader( yandexMapsURL.openStream())); String currentInfo; StringBuilder responseStrBuilder = new StringBuilder(); while ((currentInfo = in.readLine()) != null) { responseStrBuilder.append(currentInfo); } JSONObject locationInfo = new JSONObject(responseStrBuilder.toString()) .getJSONObject("response") .getJSONObject("GeoObjectCollection"); if (Integer.parseInt(locationInfo .getJSONObject("metaDataProperty") .getJSONObject("GeocoderResponseMetaData") .getString("found")) == 0) { throw new InvalidLocationException("Unknown place"); } locationInfo = locationInfo .getJSONArray("featureMember") .getJSONObject(0) .getJSONObject("GeoObject") .getJSONObject("Point"); String[] coordinates = locationInfo.getString("pos").split(" "); return new Location( Double.parseDouble(coordinates[1]), Double.parseDouble(coordinates[0])); } catch (IOException | JSONException e) { System.err.println(e.getMessage()); ++numberOfTries; } } while (numberOfTries < TwitterStream.MAX_NUMBER_OF_TRIES); throw new LocationDefinitionErrorException(LOCATION_DEFINITION_ERROR); }

Target:
private Location resolvePlaceLocationYandex(String nameOfLocation) throws InvalidLocationException, LocationDefinitionErrorException, MalformedURLException { int numberOfTries = 0; do { URL yandexMapsURL = null; try { URI uri = new URI("https", "geocode-maps.yandex.ru", "/1.x/", "geocode=" + nameOfLocation + "&key=" + yandexMapsKey + "&format=json", null); yandexMapsURL = uri.toURL(); } catch (URISyntaxException e) { throw new LocationDefinitionErrorException("Yandex: " + "Can't make valid" + "url from place. Perhaps, strange symbols are used: " + e.getMessage()); } try { String currentInfo = HttpReader.httpGet(yandexMapsURL.toString()); JSONObject locationInfo = new JSONObject(currentInfo); if (locationInfo.has("error")) { throw new LocationDefinitionErrorException("Yandex: " + locationInfo.getJSONObject("error") .getString("message")); } locationInfo = locationInfo .getJSONObject("response") .getJSONObject("GeoObjectCollection"); if (Integer.parseInt(locationInfo .getJSONObject("metaDataProperty") .getJSONObject("GeocoderResponseMetaData") .getString("found")) == 0) { throw new InvalidLocationException("Yandex: Unknown place"); } locationInfo = locationInfo .getJSONArray("featureMember") .getJSONObject(0) .getJSONObject("GeoObject") .getJSONObject("Point"); String[] coordinates = locationInfo.getString("pos").split(" "); return new Location( Double.parseDouble(coordinates[1]), Double.parseDouble(coordinates[0]), nameOfLocation); } catch (JSONException | IllegalStateException e) { ++numberOfTries; if (numberOfTries == TwitterStream.MAX_NUMBER_OF_TRIES) { String additionalText = ""; throw new LocationDefinitionErrorException( "Yandex: " + LOCATION_DEFINITION_ERROR + " : " + e.getMessage()); } } } while (numberOfTries < TwitterStream.MAX_NUMBER_OF_TRIES); throw new LocationDefinitionErrorException(LOCATION_DEFINITION_ERROR); }