0
我嘗試這樣做:我需要3個嘗試塊進行網址編碼嗎?
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) {…}
檢查異常的另一個問題。你知道一個錯誤不會發生,但編譯器強迫你寫無用的廢話。 – doublep 2012-03-11 23:06:41