美文网首页
Android 请求接口传递参数未json小结

Android 请求接口传递参数未json小结

作者: 全球顶尖伪极客 | 来源:发表于2018-07-05 19:05 被阅读0次
 /**
     * 生成JsonString格式的参数
     *
     * @param map
     * @return
     */
    public static String generateJsonStringArg(Map<String, String> map,String methodValue, boolean paramsIncludeAppKey, String loginidArg) {

        JSONObject paramsJson = new JSONObject();
        try {

            if (map != null) {
                Set<Map.Entry<String, String>> entrySet = map.entrySet();
                Iterator<Map.Entry<String, String>> ite = entrySet.iterator();
                while (ite.hasNext()) {
                    Map.Entry<String, String> entry = ite.next();
                    paramsJson.put(entry.getKey(), entry.getValue());
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return generateJsonStringArg(paramsJson, methodValue, paramsIncludeAppKey, loginidArg);

    }

    public static String generateJsonStringArg(JSONObject paramsJson, String methodValue, boolean paramsIncludeAppKey, String loginidArg) {


        try {
            if (paramsIncludeAppKey) {
                paramsJson.put(AppKey_Name, AppKey_Value);
            }

            Map<String, String> sortMap = new HashMap<String, String>();
            sortMap.put(Method_Name, methodValue);
            sortMap.put(AppKey_Name, AppKey_Value);
            sortMap.put(V_Name, V_Value);
            sortMap.put(
                    LoginId_Name,
                    EncodeUtils.hexEncode(Cryptos.aesEncrypt(
                            loginidArg.getBytes(),
                            EncodeUtils.hexDecode(secretKey),
                            EncodeUtils.hexDecode(algorithmParam))));

            String paramTmp = Base64.encodeBase64String(paramsJson.toString()
                    .getBytes("UTF-8"));
            sortMap.put(Params_Name, paramTmp);

            String sortString = mapSort2String(sortMap) + UserManager.getUser().getCertifiedValue();

            String sigValue = EncodeUtils.hexEncode(Cryptos.aesEncrypt(
                    sortString.getBytes("UTF-8"),
                    EncodeUtils.hexDecode(secretKey),
                    EncodeUtils.hexDecode(algorithmParam)));

            sortMap.put(Sig_Name, sigValue);

            JSONObject jsonArg = new JSONObject(sortMap);

            return jsonArg.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    private static String mapSort2String(Map<String, String> params) {
        StringBuilder sb = new StringBuilder();
        Object[] keys = params.keySet().toArray();
        Arrays.sort(keys);
        for (Object key1 : keys) {
            sb.append(key1).append(params.get(key1));
        }
        return sb.toString();
    }
image.png

相关文章

网友评论

      本文标题:Android 请求接口传递参数未json小结

      本文链接:https://www.haomeiwen.com/subject/wgzpuftx.html