Java URL Query Parse parseQuery(String queryString)

Here you can find the source of parseQuery(String queryString)

Description

Get properties from a URI query string.

License

Apache License

Parameter

Parameter Description
queryString the string value returned from a call to the URI class getQuery method.

Exception

Parameter Description
Exception if an error occurs while parsing the query options.

Return

Map of properties from the parsed string.

Declaration

public static Map<String, String> parseQuery(String queryString) throws Exception 

Method Source Code

//package com.java2s;
/**/*from w ww  . j  a v a2  s . c  om*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.URLDecoder;

import java.util.Collections;
import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * Get properties from a URI query string.
     *
     * @param queryString
     *        the string value returned from a call to the URI class getQuery method.
     *
     * @return <Code>Map</Code> of properties from the parsed string.
     *
     * @throws Exception if an error occurs while parsing the query options.
     */
    public static Map<String, String> parseQuery(String queryString) throws Exception {
        if (queryString != null && !queryString.isEmpty()) {
            Map<String, String> rc = new HashMap<String, String>();
            String[] parameters = queryString.split("&");
            for (int i = 0; i < parameters.length; i++) {
                int p = parameters[i].indexOf("=");
                if (p >= 0) {
                    String name = URLDecoder.decode(parameters[i].substring(0, p), "UTF-8");
                    String value = URLDecoder.decode(parameters[i].substring(p + 1), "UTF-8");
                    rc.put(name, value);
                } else {
                    rc.put(parameters[i], null);
                }
            }
            return rc;
        }

        return Collections.emptyMap();
    }
}

Related

  1. parseQuery(String query)
  2. parseQuery(String query)
  3. parseQuery(String query)
  4. parseQuery(String query)
  5. parseQuery(String query)
  6. parseQueryParameters( String query)
  7. parseQueryParameters( String queryString, String encoding)
  8. parseQueryString(final String newQueryString, final String encoding)
  9. parseQueryString(String input)