在Android中使用md5使用密钥和秘密在休息调用中获取错误?

时间:2012-11-04 19:19:37

标签: android md5

如何使用android中的md5使用密钥和秘密进行休息调用? 但我在loginService()方法中得到一个错误,

错误:     {“status”:“error”,“message”:“Signature mismatch”}。

我遵循的步骤, 步骤1-我使用md5(string)方法获取了一个字符串“param”来获取哈希字符串 步骤2-调用了一个方法loginService(),用于从Web服务获取响应

致电代码:

{ 
    String param = "name" + first_name + last_name  + "email" + email + "facebook_user_id" + facebook_user_id + "location" + location + "zip_code" + zip_code + "birthday" + birthday + "time" + time + "api_key" + api_key;
    String signature = md5(param);
    Post_Method_Interface commonPost = new CommonPostMethod();
    getResponceForParse = commonPost.loginService(
                first_name + last_name, email,facebook_user_id, location, zip_code, birthday,time, api_key, signature);
    Log.d(">>> GetResponceForParse >>> ",getResponceForParse);

    if (!facebook_user_id.equals(null)) {
            Post_Method_Interface commonPost = new CommonPostMethod();
            getResponceForParse = commonPost
                                .FacebookLoginModel(first_name + last_name,
                                        email, facebook_user_id, location,
                                        zip_code, birthday, time, api_key,
                                        signature);
            Log.d("GetResponceForParse 1", getResponceForParse);
                        Intent i = new Intent(getApplicationContext(),
                                HomeActivity.class);

}

post_method_Interface类的loginService()Web服务方法,

public String loginService(String name, String email,
        String facebook_user_id, String location, String zip_code,
        String birthday, String timestamp, String api_key, String sig) {

    try {
        JSONObject schObject = new JSONObject();
        schObject.put("name", name);
        schObject.put("email", email);
        schObject.put("facebook_user_id", facebook_user_id);
        schObject.put("location", location);
        schObject.put("zip_code", zip_code);
        schObject.put("birthday", birthday);
        schObject.put("time", timestamp);
        schObject.put("api_key", api_key);
        schObject.put("sig", sig);
        String URL = ApiConstant.URL_FacebookLoginModel;
        JsonPostRequest postrequest = new JsonPostRequest();
        InputStream is = postrequest.doPost(schObject, URL);
        response = postrequest.inputSteamToString(is);
        Log.d("Login", response);
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return response;

}

md5()方法:

private static final String md5(final String parem) {
try {
    // Create MD5 Hash
    MessageDigest digest = java.security.MessageDigest
            .getInstance("MD5");
    digest.update(parem.getBytes());
    byte messageDigest[] = digest.digest();

    // Create Hex String
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < messageDigest.length; i++) {
        String h = Integer.toHexString(0xFF & messageDigest[i]);
        while (h.length() < 2)
            h = "0" + h;
        hexString.append(h);
    }
    return hexString.toString();
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}
return "";

}

请帮帮我吧!

0 个答案:

没有答案