Android URL Encode weakUrlEncode(String url)

Here you can find the source of weakUrlEncode(String url)

Description

Doesn't encode characters such as: ":", ";", etc.

Declaration

public static String weakUrlEncode(String url) 

Method Source Code

//package com.java2s;

public class Main {
    /** /*from  w w w .  j  a v  a  2 s. c om*/
     * Doesn't encode characters such as: ":", ";", etc.
     * Behaves like Cocoa's stringByEscaping function. We mostly use this to ensure
     * server compatibility but ideally the server would grow up. :)
     */
    public static String weakUrlEncode(String url) {
        if (url == null)
            return null;

        // make sure you don't have a duplicate character in both lists or it will break.
        String[] reserved = { ";", "?", " ", "&", "=", "$", ",", "[", "]",
                "#", "!", "'", "(", ")", "*" };

        String[] escaped = { "%3B", "%3F", "+", "%26", "%3D", "%24", "%2C",
                "%5B", "%5D", "%21", "%27", "%28", "%28", "%29", "%2A" };

        StringBuilder encUrl = new StringBuilder(url);

        //cycle through and keep replacing (bit inefficient, but sue me!)
        for (int i = 0; i < escaped.length; i++) {
            String res = reserved[i];
            String esc = escaped[i];

            // replace all occurrences
            int index = encUrl.indexOf(res);
            while (index != -1) {
                encUrl.replace(index, index + res.length(), esc);

                index = encUrl.indexOf(res);
            }
        }
        return encUrl.toString();
    }
}

Related

  1. encodeParams(ArrayList params)
  2. encodeUrl(Bundle parameters)
  3. encodeUriSegment(String segment)
  4. encodeUrl(Bundle parameters)
  5. encodeUrlParam(String s)
  6. encode(String s)
  7. getParams(Map params, String encode)
  8. encodeParameters(Map params)
  9. escapeURL(String link)