我试过了:
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 };
}
但它将网址变为:
http%3A%2F%2Fmaps.googleapis.com%2Fmaps%2Fapi%2Fgeocode%2Fxml%3Faddress%3DCanada%2BAlberta%2BGrande+Cache%26sensor%3Dfalse
所以我只想编码国家,省和城市。在一个试块中处理它是不是很糟糕?像:
try {
country = URLEncoder.encode(country, "UTF-8");
province = URLEncoder.encode(province, "UTF-8");
city = URLEncoder.encode(city, "UTF-8");
}
catch (Exception e) {…}
答案 0 :(得分:1)
如果您传递的是“UTF-8”,则不会抛出UnsupportedEncodingException,因为它始终存在。因此,您可以愉快地处理所有这一切。