Java URL Encode encode(Object parameter)

Here you can find the source of encode(Object parameter)

Description

URL encode a single query parameter.

License

Apache License

Parameter

Parameter Description
parameter The query parameter to encode. This object will not be changed.

Return

The URL encoded string representation of the parameter. If the parameter is null, returns null.

Declaration

public static String encode(Object parameter) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class Main {
    /**/* ww w .  j  av  a2 s . c o m*/
     * URL encode a single query parameter.
     * @param parameter The query parameter to encode. This object will not be
     *                  changed.
     * @return The URL encoded string representation of the parameter. If the
     *         parameter is null, returns null.
     */
    public static String encode(Object parameter) {
        if (parameter == null) {
            return null;
        }
        try {
            return URLEncoder.encode(parameter.toString(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // Should never happen, UTF-8 is always supported
            throw new RuntimeException(e);
        }
    }
}

Related

  1. encode(final String raw)
  2. encode(final String s, final String enc)
  3. encode(final String string)
  4. encode(final String value, final String charset)
  5. encode(HashMap map)
  6. encode(Object value)
  7. encode(Properties prop)
  8. encode(String _string, String _sEncoding)
  9. encode(String from, String to, String word)