Parsing query strings on Android

前端 未结 25 1280
时光说笑
时光说笑 2020-11-22 17:41

Java EE has ServletRequest.getParameterValues().

On non-EE platforms, URL.getQuery() simply returns a string.

What\'s the normal way to properly parse the qu

25条回答
  •  余生分开走
    2020-11-22 18:06

    This works for me.. I'm not sure why every one was after a Map, List> All I needed was a simple name value Map.

    To keep things simple I used the build in URI.getQuery();

    public static Map getUrlParameters(URI uri)
        throws UnsupportedEncodingException {
        Map params = new HashMap();
        for (String param : uri.getQuery().split("&")) {
            String pair[] = param.split("=");
            String key = URLDecoder.decode(pair[0], "UTF-8");
            String value = "";
            if (pair.length > 1) {
                value = URLDecoder.decode(pair[1], "UTF-8");
            }
            params.put(new String(key), new String(value));
        }
        return params;
    }
    

提交回复
热议问题