Android Utililty Methods URL Encode

List of utility methods to do URL Encode

Description

The list of methods to do URL Encode are organized into topic(s).

Method

Stringencode(String s)
encode
return URLEncoder.encode(s);
ArrayListencodeParams(ArrayList params)
encode Params
ArrayList<String> encoded = new ArrayList<String>();
for (String string : params) {
    String[] parts = string.split("=");
    if (parts.length == 2) {
        encoded.add(parts[0] + "="
                + URLEncoder.encode(parts[1], "UTF-8"));
    } else {
        encoded.add(string);
...
StringencodeUrl(Bundle parameters)
encode Url
if (parameters == null) {
    return "";
try {
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (String key : parameters.keySet()) {
        if (first)
...
StringencodeUriSegment(String segment)
encode Uri Segment
try {
    return URLEncoder.encode(segment, "UTF-8");
} catch (UnsupportedEncodingException e) {
    throw new IllegalStateException(e);
StringencodeUrl(Bundle parameters)
encode Url
if (parameters == null) {
    return "";
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String key : parameters.keySet()) {
    if (first) {
        first = false;
...
StringencodeUrlParam(String s)
encode Url Param
try {
    return URLEncoder.encode(s.replace("/", "%2F"), "UTF-8")
            .replace("+", "%20");
} catch (UnsupportedEncodingException e) {
    return s;
StringweakUrlEncode(String url)
Doesn't encode characters such as: ":", ";", etc.
if (url == null)
    return null;
String[] reserved = { ";", "?", " ", "&", "=", "$", ",", "[", "]",
        "#", "!", "'", "(", ")", "*" };
String[] escaped = { "%3B", "%3F", "+", "%26", "%3D", "%24", "%2C",
        "%5B", "%5D", "%21", "%27", "%28", "%28", "%29", "%2A" };
StringBuilder encUrl = new StringBuilder(url);
for (int i = 0; i < escaped.length; i++) {
...
Stringencode(String s)
encode
if (s == null) {
    return "";
try {
    return URLEncoder.encode(s, "UTF-8").replace("+", "%20")
            .replace("*", "%2A").replace("%7E", "~")
            .replace("#", "%23");
} catch (UnsupportedEncodingException e) {
...
StringgetParams(Map params, String encode)
get Params
StringBuffer stringBuffer = new StringBuffer();
if (params != null && !params.isEmpty()) {
    for (Map.Entry<String, String> entry : params.entrySet()) {
        try {
            if (entry.getValue() == null
                    || "".equals(entry.getValue())) {
                stringBuffer.append(entry.getKey()).append("=")
                        .append(entry.getValue()).append("&");
...
StringencodeParameters(Map params)
encode Parameters
StringBuilder body = new StringBuilder();
Iterator<Entry<String, String>> it = params.entrySet().iterator();
boolean firstElement = true;
while (it.hasNext()) {
    Map.Entry<String, String> pair = it.next();
    if (firstElement) {
        firstElement = false;
    } else {
...