Parsing query strings on Android

前端 未结 25 1303
时光说笑
时光说笑 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 17:49

    Here is BalusC's answer, but it compiles and returns results:

    public static Map> getUrlParameters(String url)
            throws UnsupportedEncodingException {
        Map> params = new HashMap>();
        String[] urlParts = url.split("\\?");
        if (urlParts.length > 1) {
            String query = urlParts[1];
            for (String param : query.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");
                }
                List values = params.get(key);
                if (values == null) {
                    values = new ArrayList();
                    params.put(key, values);
                }
                values.add(value);
            }
        }
        return params;
    }
    

提交回复
热议问题