I tried this:
String query = "http://maps.googleapis.com/maps/api/geocode/xml?address="+country+"+"+province+"+"+city+"&sensor=false";
try {
query = URLEncoder.encode(query, "UTF-8");
}
catch (Exception e) {
println("getLatLonFromAdress URLEncoder error: "+e);
return new float[] { -1f, -1f };
}
but it turns the url into:
http%3A%2F%2Fmaps.googleapis.com%2Fmaps%2Fapi%2Fgeocode%2Fxml%3Faddress%3DCanada%2BAlberta%2BGrande+Cache%26sensor%3Dfalse
So I only want to encode country, province and city. Is it bad to handle that in one try block? Like:
try {
country = URLEncoder.encode(country, "UTF-8");
province = URLEncoder.encode(province, "UTF-8");
city = URLEncoder.encode(city, "UTF-8");
}
catch (Exception e) {…}
The UnsupportedEncodingException will not be thrown if you are passing “UTF-8” as that will always be present. Therefor you can happily handle them all in 1 catch.