Here you can find the source of encodeParameter(String key, String value)
Parameter | Description |
---|---|
key | The key to encode. |
value | The value to encode |
Parameter | Description |
---|---|
UnsupportedEncodingException | If any error occurs while encoding the parameter. |
public static String encodeParameter(String key, String value) throws UnsupportedEncodingException
//package com.java2s; /* Copyright 2010 Marcel Overdijk * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License./*from w w w . jav a2 s .co m*/ */ import java.io.UnsupportedEncodingException; import java.net.URLEncoder; public class Main { /** * Encodes to given key to: * <p> * <code><encoded key>=<encoded value></code> * </p> * * @param key The key to encode. * @param value The value to encode * @return The encoded parameter. * @throws UnsupportedEncodingException If any error occurs while encoding the parameter. * @see #encode(String) * @see #encodeParamters(Map) */ public static String encodeParameter(String key, String value) throws UnsupportedEncodingException { StringBuffer sb = new StringBuffer(); sb.append(encode(key)); sb.append("="); sb.append(encode(value)); return sb.toString(); } /** * Encodes the given value to UTF-8. * * @param value The value to encode. * @return The encoded value. * @throws UnsupportedEncodingException If any error occurs while encoding the value. * @see #encodeParameter(String, String) * @see #encodeParamters(Map) */ public static String encode(String value) throws UnsupportedEncodingException { if (value != null && value.length() > 0) { return URLEncoder.encode(value, "UTF-8"); } else { return ""; } } }