Java URL Query Parse parseQueryString(String queryString, Map params)

Here you can find the source of parseQueryString(String queryString, Map params)

Description

This method parses a query string

License

Open Source License

Parameter

Parameter Description
queryString the query string
params a map to put the values in

Declaration

public static void parseQueryString(String queryString, Map<String, String> params) 

Method Source Code

//package com.java2s;
/**/*from w ww  . jav a  2s .co  m*/
 * This file is part of CubeEngine.
 * CubeEngine is licensed under the GNU General Public License Version 3.
 *
 * CubeEngine is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CubeEngine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CubeEngine.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import java.util.Map;

import java.util.StringTokenizer;

public class Main {
    /**
     * This method parses a query string
     *
     * @param queryString the query string
     * @param params      a map to put the values in
     */
    public static void parseQueryString(String queryString, Map<String, String> params) {
        if (queryString == null || params == null) {
            return;
        }
        if (queryString.length() > 0) {
            String token;
            int offset;
            StringTokenizer tokenizer = new StringTokenizer(queryString, "&");
            while (tokenizer.hasMoreTokens()) {
                token = tokenizer.nextToken();
                if ((offset = token.indexOf('=')) > 0) {
                    params.put(urlDecode(token.substring(0, offset)), urlDecode(token.substring(offset + 1)));
                } else {
                    params.put(urlDecode(token), null);
                }
            }
        }
    }

    /**
     * Decodes the percent encoding scheme.
     *
     * For example: "an+example%20string" -> "an example string"
     */
    public static String urlDecode(String string) {
        if (string == null) {
            return null;
        }
        try {
            return URLDecoder.decode(string, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return string;
        }
    }
}

Related

  1. parseQueryString(String query)
  2. parseQueryString(String query, boolean ignoreEmpty)
  3. parseQueryString(String queryString)
  4. parseQueryString(String queryString)
  5. parseQuerystring(String queryString)
  6. parseQueryStringEx(String queryString)
  7. parseURLQuery(String query)