您的位置:首页 > 移动开发 > Android开发

Android使用commons-codec-1.6 遇到的问题

2013-09-25 16:42 295 查看
由于需要用到Android 中的md5加密,之前在服务端使用该jar觉得很赞,于是导入android工程也使用,出现问题了,

找不到encodeHexString()方法,

后来在万能的stackoverflow 找到解决方案:

String s = DigestUtils.md5Hex(data);


Replace this statement with the following and it will work:
String s = new String(Hex.encodeHex(DigestUtils.md5(data)));


Similarly, for shaHex exampl, you can change it to
String hash = new String(Hex.encodeHex(DigestUtils.sha("textToHash");


This works because even though Android does not have encodeHexString(), it does have encodeHex(). Hope this would help others who run into the same issue.

根本原因应该是android内部库也有一个org.apache.commons.codecs.*的 jar,导致包名冲突,而优先加载了系统的,而系统的jar包中却没有encodeHexString():

解决方案:

方案1
以上的解决方法,做encodeHexString的工作吧,encodeHexString没有,encodeHex还是有的。

方案2.源码重新编译,改下报名为org.myapache.commons.codecs,这下不冲突了,哈哈。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: