Java URL Build queryString(final Map> params)

Here you can find the source of queryString(final Map> params)

Description

Encode URL parameters as a query string.

License

Apache License

Parameter

Parameter Description
params Query parameters

Declaration

private static String queryString(final Map<String, List<String>> params) 

Method Source Code

//package com.java2s;
/**//from w  w w  .ja v  a  2 s  .  co m
 * Copyright 2014 DuraSpace, Inc.
 *
 * Licensed 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.URLEncoder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Main {
    /**
     * Encode URL parameters as a query string.
     * @param params Query parameters
    **/
    private static String queryString(final Map<String, List<String>> params) {
        final StringBuilder builder = new StringBuilder();
        if (params != null && params.size() > 0) {
            for (final Iterator<String> it = params.keySet().iterator(); it.hasNext();) {
                final String key = it.next();
                final List<String> values = params.get(key);
                for (final String value : values) {
                    try {
                        builder.append(key + "=" + URLEncoder.encode(value, "UTF-8") + "&");
                    } catch (final UnsupportedEncodingException e) {
                        builder.append(key + "=" + value + "&");
                    }
                }
            }
            return builder.length() > 0 ? "?" + builder.substring(0, builder.length() - 1) : "";
        }
        return "";
    }
}

Related

  1. buildURLForParams(String... urlParams)
  2. buildURLForParamsWithPrefix(String prefix, String... urlParams)
  3. buildURLForPath(String[] _path)
  4. buildUrlFromParts(String... parts)
  5. buildUrlWithFilter(String url, String[] filterArrayString)
  6. queryString(final Map values)
  7. queryStringFromMap(Map map)
  8. queryStringify(HashMap qString)
  9. queryStringToMap(String query, Boolean decode)