Urlencoder implementation classes in the Java me platform

Source: Internet
Author: User
Tags final html form alphanumeric characters

This class was modified from the Java.net.URLEncoder to perform the normal URL coding work, tested on several handsets. When used directly call Urlencoder.encode ("China") if sent to the server side. You can encode Chinese in the following ways, and then send to the server. String data = "para=" +urlencoder.encode ("parameters");

Outputstream.write (Data.getbytes ());

.......

On the server side, take the servlet as an example request.getparameter ("Para") to get "parameters"

Package com.j2medev.httpme.tools;

Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.OutputStreamWriter;
Import java.io.UnsupportedEncodingException;
/**
* Utility class for Form Encoding.this class be modified form Java.net.URLEncoder So, it can work in CLDC env.
* This class contains static methods
* For converting a String to the <CODE>application/x-www-form-urlencoded</CODE> MIME
* format. For more information about HTML form encoding, consult the HTML
* <a href= "http://www.w3.org/TR/html4/" &GT;SPECIFICATION&LT;/A&GT;.
*
* <p>
* When encoding a String, the following rules apply:
*
* <p>
* <ul>
* <li>the alphanumeric characters "<code>a</code>" through
* "<code>z</code>", "<code>A</code>" through
* "<code>Z</code>" and "<code>0</code>"
* through "<code>9</code>" remain the same.
* <li>the special characters "<code>.</code>",
* "<code>-</code>", "<code>*</code>", and
* "<code>_</code>" remain the same.
* <li>the space character ' <code> </code> ' is
* converted into a plus sign "<code>+</code>".
* <li>all Other characters are unsafe and are
* One or more bytes using some encoding scheme. Then each byte
* Represented by the 3-character string
* "<code>%<i>xy</i></code>", where <i>xy</i> is the
* Two-digit hexadecimal representation of the byte.
* The recommended encoding scheme to use is UTF-8. However,
* For compatibility reasons, if a encoding is not specified,
* Then the default encoding of the platform is used.
* </ul>
*
* <p>
* For example using UTF-8 as the encoding scheme the string "the
* Stringü@foo-bar "would get converted to
* "The+string+%c3%bc%40foo-bar" because in UTF-8 the character
*üis encoded as two bytes C3 (hex) and BC (hex), and the
* Character @ is encoded as one byte (hex).
*
* @author Mingjava
* @version 0.1 05/06/2006
* @since HTTPME 0.1
*/
public class Urlencoder {

/** the characters which does not need to be encoded. */
private static boolean[] dontneedencoding;
private static String Defaultencname = "";
static final int casediff = (' A '-' a ');
static {
dontneedencoding = new boolean[256];
int i;
for (i = ' a '; I <= ' z '; i++) {
Dontneedencoding[i] = true;
}
for (i = ' A '; I <= ' Z '; i++) {
Dontneedencoding[i] = true;
}
for (i = ' 0 '; I <= ' 9 '; i++) {
Dontneedencoding[i] = true;
}
dontneedencoding['] = true; Encoding a spaces to A + are done by the Encode () method
dontneedencoding['-'] = true;
Dontneedencoding[' _ '] = true;
dontneedencoding['. '] = true;
dontneedencoding[' * '] = true;
Defaultencname = System.getproperty ("microedition.encoding");
if (Defaultencname = null | | Defaultencname.trim (). Length () = 0) {
Defaultencname = "UTF-8";
}
}

public static final int min_radix = 2;

/**
* The maximum radix available for conversion to and from strings.
*/
public static final int max_radix = 36;
/**
* The class is ' not ' meant to be instantiated.
*/
Private Urlencoder () {}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.