Whenever i am trying to encode or decode a string using UTF - 8 it is showing me Unhandled Exception: UnsupportedEncodingException.
So Android Studio is giving me two solution, which are
1) Use throws UnsupportedEncodingException 2) put that piece of code between try catch with UnsupportedEncodingException as catch argument..
So which one is good practice to use and why ?
public static String getEncodedString(String strOriginal) throws UnsupportedEncodingException {
byte[] dataFirstName = strOriginal.getBytes("UTF-8");
return Base64.encodeToString(dataFirstName, Base64.DEFAULT);
}
OR
public static String getEncodedString(String strOriginal) {
byte[] dataFirstName = new byte[0];
try {
dataFirstName = strOriginal.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return Base64.encodeToString(dataFirstName, Base64.DEFAULT);
}