Java URL Parameter Builder serializeParameters(final Map params)

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

Description

Serialises the specified map of parameters into a URL query string.

License

Apache License

Parameter

Parameter Description
params A map of the URL query parameters. May be empty or null .

Return

The serialised URL query string, empty if no parameters.

Declaration

public static String serializeParameters(final Map<String, String> params) 

Method Source Code


//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
     * &amp;client_id=s6BhdRkqt3
     * &amp;state=xyz
     * &amp;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();
    }
}

Related

  1. parseParameters(final String value)
  2. parseResponseParams(String body)
  3. sendGetRequest(String path, Map params, String enc)
  4. sendPostRequest(String path, Map params, String enc)
  5. serializeParameters(final Map parameters)
  6. stringToMap(String input)
  7. stringToMap(String input)
  8. stringUncode(String param)