Java URL Query Build createQueryString(Map options)

Here you can find the source of createQueryString(Map options)

Description

Given a key / value mapping, create and return a URI formatted query string that is valid and can be appended to a URI.

License

Apache License

Parameter

Parameter Description
options The Mapping that will create the new Query string.

Exception

Parameter Description
URISyntaxException an exception

Return

a URI formatted query string.

Declaration

public static String createQueryString(Map<String, ? extends Object> options) throws URISyntaxException 

Method Source Code

//package com.java2s;
/**/*  w w  w  .  j  ava 2s . co m*/
 * 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.io.UnsupportedEncodingException;

import java.net.URISyntaxException;

import java.net.URLEncoder;

import java.util.Map;

public class Main {
    /**
     * Given a key / value mapping, create and return a URI formatted query string that is valid
     * and can be appended to a URI.
     *
     * @param options
     *        The Mapping that will create the new Query string.
     *
     * @return a URI formatted query string.
     *
     * @throws URISyntaxException
     */
    public static String createQueryString(Map<String, ? extends Object> options) throws URISyntaxException {
        try {
            if (options.size() > 0) {
                StringBuffer rc = new StringBuffer();
                boolean first = true;
                for (String key : options.keySet()) {
                    if (first) {
                        first = false;
                    } else {
                        rc.append("&");
                    }
                    String value = (String) options.get(key);
                    rc.append(URLEncoder.encode(key, "UTF-8"));
                    rc.append("=");
                    rc.append(URLEncoder.encode(value, "UTF-8"));
                }
                return rc.toString();
            } else {
                return "";
            }
        } catch (UnsupportedEncodingException e) {
            throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
        }
    }
}

Related

  1. createQueryString(Map params)
  2. createQueryString(Map params)
  3. createQueryStringForParameters(Map parameters)
  4. createQueryStringFromMap(Map m, String ampersand, boolean encode)