URL Encode Utils : Url « Network « Android






URL Encode Utils

 
//package weibo4android.util;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.BitSet;
/**
 * @author yuanming
 * 
 */
public class URLEncodeUtils {

  static BitSet dontNeedEncoding;

  static {

    /*
     * The list of characters that are not encoded has been determined as
     * follows:
     * 
     * RFC 2396 states: ----- Data characters that are allowed in a URI but
     * do not have a reserved purpose are called unreserved. These include
     * upper and lower case letters, decimal digits, and a limited set of
     * punctuation marks and symbols.
     * 
     * unreserved = alphanum | mark
     * 
     * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
     * 
     * Unreserved characters can be escaped without changing the semantics
     * of the URI, but this should not be done unless the URI is being used
     * in a context that does not allow the unescaped character to appear.
     * -----
     * 
     * It appears that both Netscape and Internet Explorer escape all
     * special characters from this list with the exception of "-", "_",
     * ".", "*". While it is not clear why they are escaping the other
     * characters, perhaps it is safest to assume that there might be
     * contexts in which the others are unsafe if not escaped. Therefore, we
     * will use the same list. It is also noteworthy that this is consistent
     * with O'Reilly's "HTML: The Definitive Guide" (page 164).
     * 
     * As a last note, Intenet Explorer does not encode the "@" character
     * which is clearly not unreserved according to the RFC. We are being
     * consistent with the RFC in this matter, as is Netscape.
     */

    dontNeedEncoding = new BitSet(256);
    int i;
    for (i = 'a'; i <= 'z'; i++) {
      dontNeedEncoding.set(i);
    }
    for (i = 'A'; i <= 'Z'; i++) {
      dontNeedEncoding.set(i);
    }
    for (i = '0'; i <= '9'; i++) {
      dontNeedEncoding.set(i);
    }
    dontNeedEncoding.set(' '); /*
                   * encoding a space to a + is done in the
                   * encode() method
                   */
    dontNeedEncoding.set('-');
    dontNeedEncoding.set('_');
    dontNeedEncoding.set('.');
    dontNeedEncoding.set('*');

    dontNeedEncoding.set('+');
    dontNeedEncoding.set('%');

  }

  /**
   * ?????????urlencode?
   * 
   * @param str
   * @return
   */
  public static final boolean isURLEncoded(String str) {
    if (str==null &&"".equals(str)) {
      return false;
    }
    char[] chars = str.toCharArray();
    boolean containsPercent = false;
    for (char c : chars) {
      if (Character.isWhitespace(c)) {
        return false;
      }
      if (!dontNeedEncoding.get(c)) {
        return false;
      }
      if(c == '%'){
        containsPercent = true;
      }
    }
    if(!containsPercent){
      return false;
    }
    return true;
  }

  public static final String encodeURL(String str) {
    try {
      return URLEncoder.encode(str, "utf-8");
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
  }
  public static final String decodeURL(String str) {
    try {
      return URLDecoder.decode(str, "utf-8");
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
  }
  
}

   
  








Related examples in the same category

1.Using Intent to open a URL
2.Process xml document from a Url
3.Suggest Url Provider
4.Showing android:autoLink property, which linkifies things like URLs and phone numbers found in the text.
5.extends ArrayAdapter to create URL history
6.Used to compress URL using the bit.ly service.
7.URL encode and decode
8.Is valid URL
9.Download from URLConnection
10.Request from an URL
11.Get Url From Img Tag
12.Returns the contents of the given URL as an array of strings
13.Read from a URL
14.Pull the raw text content of the given URL.
15.Get Video from URL
16.Gets data from URL
17.get Url Response
18.Downloads a file given URL to specified destination
19.get Host From Url
20.encode Url
21.decode Url
22.Convert a byte array to a URL encoded string
23.get Url content with max retries
24.get Url By Post
25.Take a base url and a {@link Map} of parameters to build a valid url