在android中尝试md5字符串时遇到麻烦

时间:2014-09-26 09:35:07

标签: android networking request md5

在Android上,当我尝试md5字符串时出现以下错误:

"Performing stop of activity that is not resumed"

我需要将md5附加到网址。

请帮忙

// md5 code

MessageDigest digest;

    try {
        digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(text.getBytes());
        byte[] a = digest.digest();
        int len = a.length;
        StringBuilder sb = new StringBuilder(len << 1);
        for (int i = 0; i < len; i++) {
            sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
            sb.append(Character.forDigit(a[i] & 0x0f, 16));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {

// e.printStackTrace();         }

//请求代码 encryptedTaskId = MDConversion.MD5(taskId);

            Log.v("inside doInBackground :: ", "inside doInBackground :: ");
            totalUrl = baseUrl + "&access_token=" + accessToken + "&id=" + encryptedTaskId;

            Log.v("fetch users to forward task url :: ", "url :: "+totalUrl);

            HttpGet get = new HttpGet(totalUrl);
            HttpClient client = new DefaultHttpClient();
            HttpResponse response;

            response = client.execute(get);

1 个答案:

答案 0 :(得分:0)

尝试使用此静态函数:

 public static final String md5(final String toEncrypt) {
    try {
        final MessageDigest digest = MessageDigest.getInstance("md5");
        digest.update(toEncrypt.getBytes());
        final byte[] bytes = digest.digest();
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(String.format("%02X", bytes[i]));
        }
        return sb.toString().toLowerCase();
    } catch (Exception exc) {
        return ""; // Impossibru!
    }
}