Java String Encode encodeParams(final Map params)

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

Description

Computes a String representing the concatenation of all the parameters.

License

Open Source License

Parameter

Parameter Description
params - The parameters to use.

Exception

Parameter Description
UnsupportedEncodingException an exception

Return

A String representing the concatenation of all the parameters.

Declaration

public static String encodeParams(final Map<String, String> params) throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/*// w w  w .j ava 2  s.  c o  m
 * This file is part of Minus-Java.
 * 
 * Minus-Java is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Minus-Java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Minus-Java. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    /**
     * Computes a <tt>String</tt> representing the concatenation of all the
     * parameters. The resulted <tt>String</tt> can be appended to an URL.
     * 
     * @param params - The parameters to use.
     * @return A String representing the concatenation of all the parameters.
     * @throws UnsupportedEncodingException
     */
    public static String encodeParams(final Map<String, String> params) throws UnsupportedEncodingException {

        final String utf8_encoding = "UTF-8";
        StringBuilder encodedURL = new StringBuilder();
        boolean isFirst = true;
        encodedURL.append("?");
        Iterator<Entry<String, String>> iter = params.entrySet().iterator();
        while (iter.hasNext()) {
            if (!isFirst) {
                encodedURL.append("&");
            }
            Entry<String, String> o = iter.next();
            encodedURL.append(URLEncoder.encode(o.getKey(), utf8_encoding));
            encodedURL.append("=");
            encodedURL.append(URLEncoder.encode(o.getValue(), utf8_encoding));
            isFirst = false;
        }
        // We must change the '+' chars to '%20'
        return encodedURL.toString().replaceAll("\\+", "%20");
    }
}

Related

  1. encodeName(String createdName)
  2. encodeParameter(String key, String value)
  3. encodeParameters(Map> parameters)
  4. encodeParameters(Map parameters)
  5. encodeParameters(Map params, String paramsEncoding)
  6. encodeParams(Map nameValuePairs)
  7. encodeParams(String encoding, String... params)
  8. EncodePath(String path)
  9. encodePath(String path)