Here you can find the source of serializeParameters(final Map
Parameter | Description |
---|---|
params | A map of the URL query parameters. May be empty or null . |
public static String serializeParameters(final Map<String, String> params)
//package com.java2s; //License from project: Apache License import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Map; public class Main { /**//from www .j av a 2 s .com * The default UTF-8 character set. */ public static final String CHARSET = "utf-8"; /** * Serialises the specified map of parameters into a URL query string. * The parameter keys and values are * {@code application/x-www-form-urlencoded} encoded. * * <p>Note that the '?' character preceding the query string in GET * requests is not included in the returned string. * * <p>Example query string: * * <pre> * response_type=code * &client_id=s6BhdRkqt3 * &state=xyz * &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb * </pre> * * <p>The opposite method is {@link #parseParameters}. * * @param params A map of the URL query parameters. May be empty or * {@code null}. * * @return The serialised URL query string, empty if no parameters. */ public static String serializeParameters(final Map<String, String> params) { if (params == null || params.isEmpty()) return ""; StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : params.entrySet()) { if (entry.getKey() == null) continue; String value = entry.getValue() != null ? entry.getValue() : ""; try { String encodedKey = URLEncoder.encode(entry.getKey(), CHARSET); String encodedValue = URLEncoder.encode(value, CHARSET); if (sb.length() > 0) sb.append('&'); sb.append(encodedKey); sb.append('='); sb.append(encodedValue); } catch (UnsupportedEncodingException e) { // UTF-8 should always be supported throw new RuntimeException(e.getMessage(), e); } } return sb.toString(); } }