Encode
Since MIDP does not have the J2SE corresponding Java.net.URLEncoder class, to send HTTP requests to the server, you must do the URL encoding yourself, refer to the JDK1.4.2 src code, and change it to a Urlencoder class that can be used in a MIDP environment:
Urlencoder.java
Package com.mboker.blog.midp.http;
Import java.io.*;
/**
* Encode URL, just like Java.net.URLEncoder.encode () in j2se.<br>
* Note:this class is modified from Java.net.URLEncoder class in J2SE 1.4.
*
* @author Xuefeng
*/
Class Urlencoder {
private static final int max_bytes_per_char = 10; Rather arbitrary limit, but safe for now
private static boolean[] dontneedencoding;
private static final int casediff = (' A '-' a ');
static {
dontneedencoding = new boolean[256];
for (int i= ' a '; i<= ' z '; i++) {
Dontneedencoding[i] = true;
}
for (int i= ' A '; i<= ' Z '; i++) {
Dontneedencoding[i] = true;
}
for (int i= ' 0 '; i<= ' 9 '; i++) {
Dontneedencoding[i] = true;
}
dontneedencoding['] = true;
dontneedencoding['-'] = true;
Dontneedencoding[' _ '] = true;
dontneedencoding['. '] = true;
dontneedencoding[' * '] = true;
}
Private Urlencoder () {}
public static string encode (string s) {
Boolean Wroteunencodedchar = false;
StringBuffer out = new StringBuffer (S.length ());
Bytearrayoutputstream buf = new Bytearrayoutputstream (Max_bytes_per_char);
OutputStreamWriter writer = new OutputStreamWriter (BUF);
for (int i = 0; i < s.length (); i++) {
int c = (int) s.charat (i);
if (c<256 && dontneedencoding[c]) {
Out.append (char) (c== "? ' + ': c));
Wroteunencodedchar = true;
} else {
Convert to external encoding before hex conversion
try {
if (Wroteunencodedchar) {//Fix for 4407610
writer = new OutputStreamWriter (BUF);
Wroteunencodedchar = false;
}
Writer.write (c);
/*
* If This character represents the start of a Unicode
* Surrogate pair, then pass in two characters. It ' s not
* Clear what should is done if a bytes reserved in the
* Surrogate pairs range occurs outside of a legal surrogate
* Pair. For now, just treat it as if it were
* character.
*/
if (c >= 0xd800 && C <= 0xDBFF) {
if ((i + 1) < S.length ()) {
int d = (int) S.charat (i + 1);
if (d >= 0xdc00 && D <= 0xDFFF) {
Writer.write (d);
i++;
}
}
}
Writer.flush ();
catch (IOException e) {
Buf.reset ();
Continue
}
byte[] ba = Buf.tobytearray ();
for (int j = 0; J < Ba.length; J + +) {
Out.append ('% ');
char ch = tohex ((Ba[j] >> 4) & 0xF);
Converting to use uppercase letter as part of
The hex value if CH is a letter.
if (Isletter (CH)) {
CH-= Casediff;
}
Out.append (CH);
ch = Tohex (Ba[j] & 0xF);
if (Isletter (CH)) {
CH-= Casediff;
}
Out.append (CH);
}
Buf.reset ();
}
}
return out.tostring ();
}
private static char tohex (int digit) {
if ((digit >= 16) | | | (Digit < 0)) {
Return ' a ';
}
if (Digit < 10) {
Return (char) (' 0 ' + digit);
}
Return (char) (' a '-+ digit);
}
private static Boolean isletter (char c) {
Return (c>= ' a ' && c<= ' z ');
}
}
J2SE's Urlencoder relies on many of the features of the java.lang.Character, removing it all and transforming it into a basic operation that can be used in a MIDP environment, and the Chinese test is all normal.