Android项目引用不到以下两个java类
import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder;
Android有自己的base64类
import android.util.Base64
//import android.util.Base64; String str = "Hello!"; //base64编码 //String strBase64 = new String(Base64.encode(str.getBytes(), Base64.DEFAULT)); String strBase64 = Base64.encodeToString(str.getBytes(), Base64.DEFAULT); //base64解码 String str2 = new String(Base64.decode(strBase64.getBytes(), Base64.DEFAULT));
用这个可以清楚一点 import android.util.Base64;import java.io.UnsupportedEncodingException;public class Base64Utils { /** * 字符Base64加密 * @param str * @return */ public static String encodeToString(String str){ try { return Base64.encodeToString(str.getBytes("UTF-8"), Base64.DEFAULT); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return ""; } /** * 字符Base64解密 * @param str * @return */ public static String decodeToString(String str){ try { return new String(Base64.decode(str.getBytes("UTF-8"), Base64.DEFAULT)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return ""; }}
发表评论