我需要將字符串轉換爲UTF-8。現在在我的代碼,我有這樣的:如何將字符串轉換爲UTF-8而不出錯catch
sb.append(URLEncoder.encode(smsAnswerText, "UTF-8"))
但編碼方法拋出異常UnsupportedEncodingException
我改寫:sb.append(smsAnswerText)
becouse寫this
,我有錯tуxt-unreadable字符
然後我試過new String(smsAnswerText.getBytes(),StandardCharsets.UTF_8)
而且這個方法也拋出異常UnsupportedEncodingException
如何將Sting轉換爲String + UTF-8而不使用UnsupportedEncodingException
?
我需要:
public static String generateBodyResponse(String smsAnswerText){
return// smsAnswerText in UTF-8
}
我
public static String generateBodyResponse(String smsAnswerText) throws UnsupportedEncodingException{
return URLEncoder.encode(smsAnswerText, "UTF-8");
}
您的意思是它會在運行時拋出異常,或者需要將它封裝到try ... catch中,因爲檢查異常? – Andremoniy
我經常使用這種方法,不想拋出這個異常。也許有一種方法來轉換沒有例外發布 – user5620472
https://stackoverflow.com/questions/10402048/is-there-a-common-java-library-that-will-handle-url-encoding-decoding-for- a-coll –