Java URL Build queryString(final Map values)

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

Description

Convert a Map to a query string.

License

Open Source License

Parameter

Parameter Description
values the map with the values <code>null</code> will be encoded as empty string, all other objects are converted to String by calling its <code>toString()</code> method.

Return

e.g. "key1=value&key2=&email=max%40example.com"

Declaration

public static String queryString(final Map<String, Object> values) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.util.Map;

public class Main {
    /**//w  w w  . ja  va 2  s .c  o m
     * Convert a Map to a query string.
     * 
     * @param values
     *            the map with the values <code>null</code> will be encoded as empty string, all other objects are
     *            converted to String by calling its <code>toString()</code> method.
     * @return e.g. "key1=value&amp;key2=&amp;email=max%40example.com"
     */
    public static String queryString(final Map<String, Object> values) {
        final StringBuilder sbuf = new StringBuilder();
        String separator = "";

        for (final Map.Entry<String, Object> entry : values.entrySet()) {
            final Object entryValue = entry.getValue();
            if (entryValue instanceof Object[]) {
                for (final Object value : (Object[]) entryValue) {
                    appendParam(sbuf, separator, entry.getKey(), value);
                    separator = "&";
                }
            } else if (entryValue instanceof Iterable) {
                for (final Object multiValue : (Iterable<?>) entryValue) {
                    appendParam(sbuf, separator, entry.getKey(), multiValue);
                    separator = "&";
                }
            } else {
                appendParam(sbuf, separator, entry.getKey(), entryValue);
                separator = "&";
            }
        }

        return sbuf.toString();
    }

    private static void appendParam(final StringBuilder sbuf, final String separator, final String entryKey,
            final Object value) {
        final String sValue = value == null ? "" : String.valueOf(value);
        sbuf.append(separator);
        sbuf.append(urlEncode(entryKey));
        sbuf.append('=');
        sbuf.append(urlEncode(sValue));
    }

    static String urlEncode(final String value) {
        try {
            return URLEncoder.encode(value, "UTF-8");
        } catch (final UnsupportedEncodingException e) {
            return value;
        }
    }
}

Related

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