Here you can find the source of encodeParams(final Map
Parameter | Description |
---|---|
params | - The parameters to use. |
Parameter | Description |
---|---|
UnsupportedEncodingException | an exception |
public static String encodeParams(final Map<String, String> params) throws UnsupportedEncodingException
//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"); } }