Java String Encode encodeParameter(String key, String value)

Here you can find the source of encodeParameter(String key, String value)

Description

Encodes to given key to:

=

License

Apache License

Parameter

Parameter Description
key The key to encode.
value The value to encode

Exception

Parameter Description
UnsupportedEncodingException If any error occurs while encoding the parameter.

Return

The encoded parameter.

Declaration

public static String encodeParameter(String key, String value) throws UnsupportedEncodingException 

Method Source Code


//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 "";
        }
    }
}

Related

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