美文网首页
url拼接参数操作

url拼接参数操作

作者: 冰楓紫憶 | 来源:发表于2021-12-07 16:32 被阅读0次
    Uri uri = Uri.parse(url);
    

    url拼接参数替换对应的值

    public static Uri replaceUriParameter(Uri uri, String key, String newValue) {
            final Set<String> params = uri.getQueryParameterNames();
            final Uri.Builder newUri = uri.buildUpon().clearQuery();
            for (String param : params) {
                newUri.appendQueryParameter(param,
                        param.equals(key) ? newValue : uri.getQueryParameter(param));
            }
    
            return newUri.build();
        }
    

    url解析拼接参数为Map

    public static Map<String, String> parseSourceParam(Uri uri) {
            Map<String, String> param = new HashMap();
            String query = NTool.getQuery(uri.toString());
            if (!TextUtils.isEmpty(query)) {
                String[] ret = query.split("&");
                if (ret != null) {
                    String[] var4 = ret;
                    int var5 = ret.length;
    
                    for(int var6 = 0; var6 < var5; ++var6) {
                        String s = var4[var6];
                        String[] kv = s.split("=");
                        if (kv != null && kv.length == 2) {
                            param.put(kv[0], kv[1]);
                        }
                    }
                }
            }
    
            return param;
        }
    
        public static String appendParam(String url, Map<String, String> param) {
            if (param != null && param.size() != 0) {
                StringBuilder sb = new StringBuilder();
                Iterator it = param.entrySet().iterator();
    
                while(it.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry)it.next();
                    sb.append((String)entry.getKey()).append("=").append((String)entry.getValue()).append("&");
                }
    
                String paramStr = sb.toString();
                if (paramStr.endsWith("&")) {
                    paramStr = paramStr.substring(0, paramStr.length());
                }
    
                return appendQuery(url, paramStr);
            } else {
                return url;
            }
        }
    
        public static String appendQuery(String url, String query) {
            if (TextUtils.isEmpty(query)) {
                return url;
            } else {
                return url.contains("?") ? url + "&" + query : url + "?" + query;
            }
        }
    

    解析Url拼接参数放入到Intent中

    public static void parseUrlParam(String query, Intent intent) {
            if (!TextUtils.isEmpty(query)) {
                String[] ret = query.split("&");
                if (ret != null && ret.length > 0) {
                    String[] var3 = ret;
                    int var4 = ret.length;
    
                    for(int var5 = 0; var5 < var4; ++var5) {
                        String kv = var3[var5];
                        String[] kvs = splitFirst(kv, "=");
                        if (kvs != null && kvs.length == 2) {
                            intent.putExtra(kvs[0], kvs[1]);
                        }
                    }
                }
    
            }
        }
    

    相关文章

      网友评论

          本文标题:url拼接参数操作

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