zoukankan      html  css  js  c++  java
  • 将map中的查询参数拼装到URL路径中

    被调接口的URL路径:

    //被调接口url
    String apiUrl = "http://api.open.xxxxxx.com/implatform/interview/send?access_token=551c619ef13c45debe92a64880f5e1cdlzJv";

    将下面的key和value放到一个map中:

    phonetype:1
    phone:15666888553
    name:张三

    将map中的key和value拼装成"&phonetype=1&phone=15666888553&name=张三"这种形式:

        public static String getUrlParamsByMap(Map<String, Object> map) {
            if (map == null) {
                return "";
            }
            StringBuffer sb = new StringBuffer();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                sb.append(entry.getKey() + "=" + entry.getValue());
                sb.append("&");
            }
            String s = sb.toString();
            if (s.endsWith("&")) {
                s = s.substring(0, s.length() - 1);
                //s = org.apache.commons.lang.StringUtils.substringBeforeLast(s, "&");
            }
            return s;
        }

    将上面的被调接口URL和拼装好的查询参数组装起来:

        //合并两部分url
        public static String urlCombine(String path1, String path2){
            if(CommonUtil.isNullOrEmpty(path1)) throw new NullArgumentException("path1");
    
            if (CommonUtil.isNullOrEmpty(path2)) path2 = "";
    
            path1 = CommonUtil.trimEnd(path1, "?");
            path1 = CommonUtil.trimEnd(path1, "&");
    
            path2 = CommonUtil.trimStart(path2,"?");
            path2 = CommonUtil.trimStart(path2,"&");
    
            if (path1.indexOf("?")>-1){
                return path1+"&"+path2;
            }
            else{
                return path1+"?"+path2;
            }
        }

    组装后就像下面这样(只是举例):

    http://api.open.xxxxxx.com/implatform/interview/send?access_token=551c619ef13c45debe92a64880f5e1cdlzJv&phonetype=1&phone=15666888553&name=张三

    如果觉得本文对您有帮助,不妨扫描下方微信二维码打赏点,您的鼓励是我前进最大的动力:

  • 相关阅读:
    PyQt5经典案例
    JS实现深拷贝(解决循环引用等问题)
    React/Vue里的key到底有什么用?看完这篇你就知道了!(附demo代码)
    linux盘符操作命令
    Ubuntu20.04下安装opencv for C++
    数字图像处理-python随机噪声、高斯噪声和椒盐噪声实现
    数字图像处理-(1)pyqt页面
    数字图像处理-Python读取BMP文件
    docker笔记
    CentOS7安装GO
  • 原文地址:https://www.cnblogs.com/jun1019/p/6763214.html
Copyright © 2011-2022 走看看