Here you can find the source of queryString(final Map
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. |
public static String queryString(final Map<String, Object> values)
//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&key2=&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; } } }