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

 /**
     * 生成JsonString格式的参数
     *
     * @param map
     * @return
     */
    public static String generateJsonStringArg(Map map,String methodValue, boolean paramsIncludeAppKey, String loginidArg) {

        JSONObject paramsJson = new JSONObject();
        try {

            if (map != null) {
                Set> entrySet = map.entrySet();
                Iterator> ite = entrySet.iterator();
                while (ite.hasNext()) {
                    Map.Entry 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 sortMap = new HashMap();
            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 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小结)