Post请求参数排序

    /**
     *  对提交的参数转正Map后转正TreeMap进行排序
     * @return
     */
//    public static Map buildOrderParamMap(String uuid, boolean rsa2,String userName,String Password) {
    public static void buildOrderParamMap(String uuid, boolean rsa2,String userName,String Password) {

        Map keyValues = new HashMap();

//    orderNo 只能是数字不能是文字描述
        keyValues.put("uuid", uuid);

        keyValues.put("account", userName);

        keyValues.put("password", Password);


        Log.e("OKGo网络请求测试post1",keyValues.toString());


//        Map map = new HashMap();
        TreeMap treemap = new TreeMap(keyValues);
        Log.e("OKGo网络请求测试post1",treemap.toString());

        buildOrderParam(treemap);

//        return keyValues;
    }



    /**
     * md5签名
     */
    public static void buildOrderParam(Map map) {
        List keys = new ArrayList(map.keySet());

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < keys.size() - 1; i++) {
            String key = keys.get(i);
            String value = map.get(key);
            sb.append(buildKeyValue(key, value, true));
            sb.append("&");
        }

        String tailKey = keys.get(keys.size() - 1);
        String tailValue = map.get(tailKey);
        sb.append(buildKeyValue(tailKey, tailValue, true));
//        Log.e("OKGo网络请求测试post1",sb.append("&key=Gemar").toString());

        String md5str = sb.append("&key=Gemar").toString();
        Log.e("OKGo网络请求测试post1111",md5str);
        md5strToken = Md5utils.getMD5Str(md5str);

        Log.e("OKGo网络请求测试post1",md5strToken.toString());

//        return md5strToken;
    }



    /**
     * 拼接键值对
     *
     */
    private static String buildKeyValue(String key, String value, boolean isEncode) {
        StringBuilder sb = new StringBuilder();
        sb.append(key);
        sb.append("=");
        if (isEncode) {
            try {
                sb.append(URLEncoder.encode(value, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                sb.append(value);
            }
        } else {
            sb.append(value);
        }
        return sb.toString();
    }

 

 

你可能感兴趣的:(Post请求参数排序)