时间:2021-04-16
由于MIDP没有J2SE对应的java.net.URLEncoder类,因此,要向服务器发送HTTP请求,必须自己进行URL编码,参考JDK1.4.2的src代码,将其改为一个能用在MIDP环境中的URLEncoder类:
// 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.
**/
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 be 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 any other
* 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 '\0';
}
if (digit < 10) {
return (char)('0' + digit);
}
return (char)('a' - 10 + digit);
}
private static boolean isLetter(char c) {
return (c>='a' && c<='z');
}
}
J2SE的URLEncoder依赖于java.lang.Character的许多特性,将其全部剔除并改造为基本运算,即可在MIDP环境中使用,中文测试也一切正常。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
今天给大家介绍一下python中一个强大的功能:抽象类因为最近发现很多同事不理解抽象类,正好抽空写篇博客,给大家介绍一下抽象类抽象类的作用就是控制子类的方法的名
用法1为原始类型扩展方法先说一下,this后面跟的类型,就是要拓展方法的类型。注意要写在静态类中的静态方法,不然有些情况下访问//////扩展类用于为原始类扩展
今天接触了一下ToStringBuilder类,这里做一个备忘。ToStringBuilder在包common-lang中,主要用于一个类的格式化输出。ToSt
定义链接样式 CSS为一些特殊效果准备了特定的工具,我们称之为“伪类”。其中有几项是我们经常用到的,下面我们就详细介绍一下经常用于定义
Python中的random模块用于生成随机数。下面介绍一下random模块中最常用的几个函数。random.randomrandom.random()用于生成