java 排序签名

在接入SDK时,通常需要签名和验签,而签名时,需要对url内容进行排序。

代码如下:

 public static String putPairsSequenceAndTogether(Map<String, String> info) {

        List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(info.entrySet());
        Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
            @Override
            public int compare(Entry<String, String> arg0, Entry<String, String> arg1) {
                // TODO Auto-generated method stub
                return (arg0.getKey()).compareTo(arg1.getKey());
            }
        });
        String ret = "";

        for (Map.Entry<String, String> entry : infoIds) {
            Log.d(TAG, "key = " + entry.getKey() + "value = " + entry.getValue());
            ret += entry.getKey();
            ret += "=";
            ret += entry.getValue();
            ret += "&";
        }
        ret = ret.substring(0, ret.length() - 1);
        Log.d(TAG, "[putPairsSequenceAndTogether] ret = " + ret);
        return ret;
    }

这个方法就是把map先按照key进行排序,然后变成 key=value&key=value的形式,在签名时,这个方法很好用

你可能感兴趣的:(android,sdk,java,排序,签名,url)