Java String Encode encodeParameters(Map> parameters)

Here you can find the source of encodeParameters(Map> parameters)

Description

Returns a properly encoded string of parameters.

License

Open Source License

Parameter

Parameter Description
parameters a parameter

Declaration

public static String encodeParameters(Map<String, List<String>> parameters) 

Method Source Code

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

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.util.List;
import java.util.Map;

public class Main {
    /**//from w  w  w. j  av a2 s. co  m
     * Returns a properly encoded string of parameters.
     *
     * @param parameters
     * @return
     */
    public static String encodeParameters(Map<String, List<String>> parameters) {
        if (parameters == null) {
            return "";
        }
        StringBuilder b = new StringBuilder();
        boolean first = true;
        for (String key : parameters.keySet()) {
            if (!first) {
                b.append("&");
            }
            first = false;

            List<String> values = parameters.get(key);
            try {
                if (values.size() == 1) {
                    String value = values.get(0);
                    b.append(URLEncoder.encode(key, "UTF-8")).append("=").append(URLEncoder.encode(value, "UTF-8"));
                } else {
                    for (String value : values) {
                        b.append(URLEncoder.encode(key + "[]", "UTF-8")).append("=")
                                .append(URLEncoder.encode(value, "UTF-8"));
                    }
                }
            } catch (UnsupportedEncodingException ex) {
                throw new Error(ex);
            }
        }
        return b.toString();
    }
}

Related

  1. encodeJobHistoryFileName(String logFileName)
  2. encodeMap(Map map, String enc)
  3. encodeName(final String name)
  4. encodeName(String createdName)
  5. encodeParameter(String key, String value)
  6. encodeParameters(Map parameters)
  7. encodeParameters(Map params, String paramsEncoding)
  8. encodeParams(final Map params)
  9. encodeParams(Map nameValuePairs)