我有以下字符串資源:的Android的String.format投擲UnknownFormatConversionException
<string formatted="false" name="residential_search_url">https://api.trademe.co.nz/v1/Search/Property/Residential.json?latitude_min=%f%26latitude_max=%f%26longitude_min=%f%26longitude_max=%f</string>
這在一個活動正在使用:
String residentialUrl = task.getSearchUrl(_map.getProjection().getVisibleRegion().latLngBounds,getString(R.string.residential_search_url));
並且所述方法被定義爲:
public String getSearchUrl(LatLngBounds bounds, String baseUrl){
return String.format(baseUrl,
bounds.southwest.latitude,
bounds.northeast.latitude,
bounds.southwest.longitude,
bounds.northeast.longitude);
}
我一直得到:
java.util.UnknownFormatConversionException: Conversion = l''
我也試過用%s
代替%f
,這沒什麼區別。
我需要轉義查詢字符串中的&符號。我怎麼做?我認爲這是%26,但顯然不是? – codedog
我弄錯了 - 事實證明它是以'&'逃脫的。現在它工作得很好。 – codedog
%26可以在大多數情況下工作,而不是當你使用字符串作爲輸入時,將%字符解釋爲格式說明符。聽起來像你避免了這個問題。 – user3109924