【发布时间】:2017-02-16 10:44:29
【问题描述】:
我需要将字符串转换为 UTF-8。现在在我的代码中我有这个:
sb.append(URLEncoder.encode(smsAnswerText, "UTF-8"))
但是encode方法抛出异常UnsupportedEncodingException
我重写:sb.append(smsAnswerText) 因为写this
我输入了错误的 tуxt -不可读字符
然后我尝试了new String(smsAnswerText.getBytes(),StandardCharsets.UTF_8)
而且这个方法也抛出异常UnsupportedEncodingException
如何在没有UnsupportedEncodingException 的情况下将 Sting 转换为 String+UTF-8?
我需要:
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 中,因为已检查异常?
-
我经常使用这个方法,不想抛出这个异常。也许有一种方法可以在不释放异常的情况下进行转换